Learners Guide (Module 02).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Core – Java Learning & Development Team Friday, November 6, 2020 1 Module – 2 : Data types and Variables 2 Agenda Identifiers Data-types Classes and Objects Naming conventions Built-in Classes Constan...

Core – Java Learning & Development Team Friday, November 6, 2020 1 Module – 2 : Data types and Variables 2 Agenda Identifiers Data-types Classes and Objects Naming conventions Built-in Classes Constants, operators Expressions and casting Control Structures 3 Identifiers in Java letters, digits, _, and $ (don't use $. Can confuse the runtime system) start with letter, _, or $ by convention: start with a letter variables and method names, lowercase with internal words capitalized e.g. honkingBigVariableName constants all caps with _ between internal words e.g. ANOTHER_HONKING_BIG_INDENTIFIER classes start with capital letter, internal words capitalized, all other lowercase e.g HonkingLongClassName 4 Data Types Primitive Data Types byte short int long float double boolean char //dataType identifier; int x; int y = 10; int z, zz; double a = 12.0; boolean done = false, prime = true; char mi = 'D'; stick with int for integers, double for real numbers Classes and Objects pre defined or user defined data types consisting of constructors, methods, and fields (constants and fields (variables) which may be primitives or objects.) 5 Java Primitive Data Types Data Type Characteristics Range byte 8 bit signed integer -128 to 127 short 16 bit signed integer -32768 to 32767 int 32 bit signed integer -2,147,483,648 to 2,147,483,647 long 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775,807 float 32 bit floating point number + 1.4E-45 to + 3.4028235E+38 double 64 bit floating point number + 4.9E-324 to + 1.7976931348623157E+308 boolean true or false NA, note Java booleans cannot be converted to or from other types char 16 bit, Unicode Unicode character, \u0000 to \uFFFF Can mix with integer types 6 What are Classes and Objects? Class is synonymous with data type Object is like a variable The data type of the Object is some Class referred to as an instance of a Class Classes contain: the implementation details of the data type and the interface for programmers who just want to use the data type Objects are complex variables usually multiple pieces of internal data various behaviors carried out via methods 7 Name conventions Java is case-sensitive; maxval, maxVal, and MaxVal are three different names Class names begin with a capital letter All other names begin with a lowercase letter Subsequent words are capitalized: theBigOne Underscores are not used in names These are very strong conventions! 8 Creating and Using Objects Declaration - DataType identifier Rectangle r1; Creation - new operator and specified constructor r1 = new Rectangle(); Rectangle r2 = new Rectangle(); Behavior - via the dot operator r2.setSize(10, 20); String s2 = r2.toString(); Refer to documentation for available behaviors (methods) 9 Built in Classes Java has a large built in library of classes with lots of useful methods String Math Integer, Character, Double System Arrays Scanner File Object Random 10 import import is a reserved word packages and classes can be imported to another class does not actually import the code (unlike the C++ include preprocessor command) statement outside the class block import java.util.ArrayList; import java.awt.Rectangle; public class Foo{ // code for class Foo } 11 More on import can include a whole package import java.util.*; or list a given class import java.util.Random; instructs the compiler to look in the package for types it can't find defined locally the java.lang.* package is automatically imported to all other classes. Not required to import classes that are part of the same project in Eclipse 12 The String Class String is a standard Java class a whole host of behaviors via methods also special (because it used so much) String literals exist (no other class has literals) String name = "Mike D."; String concatenation through the + operator String firstName = "Mike"; String lastName = "Scott"; String wholeName = firstName + lastName; Any primitive or object on other side of + operator from a String automatically converted to String 13 Standard Output To print to standard output use System.out.print( expression ); // no newline System.out.println( expression ); // newline System.out.println( ); // just a newline common idiom is to build up expression to be printed out System.out.println( "x is: " + x + " y is: " + y ); 14 Constants Literal constants - "the way you specify values that are not computed and recomputed, but remain, well, constant for the life of a program." true, false, null, 'c', "C++", 12, -12, 12.12345 Named constants use the keyword final to specify a constant scope may be local to a method or to a class By convention any numerical constant besides -1, 0, 1, or 2 requires a named constant final int NUM_SECTIONS = 3; 15 Operators Basic Assignment: = Arithmetic Operators: +, -, *, /, %(remainder) integer, floating point, and mixed arithmetic and expressions Assignment Operators: +=, -=, *=, /=, %= increment and decrement operators: ++, -- prefix and postfix. avoid use inside expressions. int x = 3; x++; 16 Expressions Expressions are evaluated based on the precedence of operators Java will automatically convert numerical primitive data types but results are sometimes surprising take care when mixing integer and floating point numbers in expressions The meaning of an operator is determined by its operands / is it integer division or floating point division? 17 Casting Casting is the temporary conversion of a variable from its original data type to some other data type. Like being cast for a part in a play or movie With primitive data types if a cast is necessary from a less inclusive data type to a more inclusive data type it is done automatically. int x = 5; double a = 3.5; double b = a * x + a / x; double c = x / 2; if a cast is necessary from a more inclusive to a less inclusive data type the class must be done explicitly by the programmer failure to do so results in a compile error. double a = 3.5, b = 2.7; int y = (int) a / (int) b; y = (int)( a / b ); y = (int) a / b; //syntax error 18 Primitive Casting Outer ring is most inclusive data type. Inner ring is least inclusive. In expressions variables and sub expressions of less inclusive data types are automatically cast to more inclusive. If trying to place expression that is more inclusive into variable that is less inclusive, explicit cast must be performed. 19 Java Control StruCtureS 20 Control Structures linear flow of control statements executed in consecutive order Decision making with if - else statements if(boolean-expression) statement; if(boolean-expression) { statement1; statement2; statement3; } A single statement could be replaced by a statement block, braces with 0 or more statements inside 21 Boolean Expressions boolean expressions evaluate to true or false Relational Operators: >, >=,

Use Quizgecko on...
Browser
Browser