Download presentation
Presentation is loading. Please wait.
1
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations and Arithmetic Expressions l Assignment Statement l Associativity of Operators l Preview: More Operators and Console Input
2
2 Format of a Java Application l A Java application consists of one or more classes A Java class consists of one ore more methods (functions) one of which must be the main method l A method is a collection of instructions (statements) describing how to carry out a particular task.. The following is a simple Java application to print the the message "Hello World!“ on the screen. public class Hello { public static void main ( String[] args ) { System.out.println("Hello World!"); }
3
3 Format of a Java Application – Contd. The Statement: public class Hello starts a new class named Hello public means the class is accessible to all other objects, we shall encounter private later. The class containing the main method must be declared as public l It is up to you to give names to your classes provided you follow the following simple rules and conventions. A class name »must be one word (no spaces) »must start with a letter »must not contain special characters (+, &, etc) »should start with capital letter »If a class name contains more than one English words, each word should start with a capital letter (e.g. MyFirstClass ) »It should reflect the function of the class A Java file must be saved with the name of the class it contains with the extension.java If it contains more than one class, the name of the class containing the main method must be used.
4
4 Format of a Java Application – Contd. The Statement: public static void main starts the main method. Again the main method must always be declared as public – most methods are private. The main method must always be declared as static, to be explained later - most methods are not static The parameter (String[],args) is required for the main method. It is used to pass command line arguments l Naming a method follow the same rule as naming a class, however, by convention, we shall always start a method with a small letter to differentiate it with a class. If a method contains more than one English word, the subsequent words should start with a capital letter. E.g. myFirstMethod The Statement: System.out.println(“Hello world”); prints a line of text “Hello world” on the standard output (the screen)
5
5 Format of a Java Application – Contd. The standard output is represented by an object called out contained in the class System and referred to as System.out println is a method of the System.out object and can be used to print strings and numbers. e.g. System.out.println(3+4); displays 7 l When ever you use a method in Java, you must specify three items: »The object containing the method (e.g. System.out ) »The name of the method (e.g. println ) »A pair of parenthesis which may contain any other information needed by the method (e.g. “Hello world”) The println method prints the contents of its argument and move to the next line. E.g. the following prints two lines. System.out.println(“ICS Department”); Syetem.out.println(“KFUPM”); Another method of the System.out object is print. This prints its argument but does not move to the next line. The following statements prints one line. System.out.print(“ICS Department”); Syetem.out.print(“KFUPM”);
6
6 Primitive Data Types Java has eight primitive data types as described below. TypeSize/Format Range (whole numbers) byte 8-bit two's complement-128 to 127 short 16-bit two's complement-32,768 to 32,767 int 32-bit two's complementabout –2 billion to 2billion long 64-bit two's complementabout –10E18 to +10E18 (real numbers) float 32-bit IEEE 754-3.4E38 to +3.4E38 double 64-bit IEEE 754 -1.7E308 to 1.7E308 (other types) char 16-bit Unicode characterA single character boolean true or falseA boolean value (true or false) Apart from these, any other information is represented in Java as an Object.
7
7 Variables Declaration You can declare a variable to hold a data value of any of the primitive types. int counter; int numStudents = 583; long longValue; long numberOfAtoms = 1237890L; float gpa; float batchAverage = 0.406F; double e; doublepi = 0.314; char gender; char grade = ‘B’; boolean safe; boolean isEmpty = true; public class Example1 { public static void main ( String[] args ) { int payAmount = 123; System.out.println("The variable contains: " + payAmount ); }
8
8 Arithmetic Operators For whole/Real numbers: OperatorUseDescription + op1 + op2 Adds op1 and op2 - op1 - op2 Subtracts op2 from op1 * op1 * op2 Multiplies op1 by op2 / op1 / op2 Divides op1 by op2 % op1 % op2 Computes the remainder of dividing op1 by op2 public class Example2 { public static void main ( String[] args ) { int hoursWorked = 40; double payRate = 10.0; System.out.println("Hours Worked: " + hoursWorked ); System.out.println("pay Amount : " + (hoursWorked * payRate) ); }
9
9 Arithmetic Operation Modes of operation : If operand1 and operand2 are of same data type, then the resultant value of the operation will be of that same data type. If operand1 and operand2 are of different data type like real and integer, then the resultant value of the operation will be of real data type. The real data type may be float or double. e.g. 1/2 gives 0 1.0/2 gives 0.5 1.0/2.0 gives 0.5 Operand1Operand2Result Real Whole Real WholeReal
10
10 Arithmetic Expressions l Definition: An expression is a sequence of variables, constants, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value. l In Java, Arithmetic expressions are evaluated very similar to the way they are evaluated in algebra. Operator Meaning Precedence - unary minus highest + unary plus highest * multiplication middle / division middle % remainder middle + addition low - subtraction low e.g 78 - 12 / 4 75 2 + 6 / 2 – 9 -4
11
11 Associativity of Operators l When operators of equal precedence appear in the same expression, associativity rule governs, which is to be evaluated first. l Left associativity rule In Java, all binary operators except for the assignment operators are evaluated in left to right order. 2 * 7 * 3 4 - 2 + 5----- 14 * 3 2 + 5 ------- 42 7 l Right associativity rule Assignment operators are evaluated right to left. int result; result = ((a + b) / 2) % 3;
12
12 Evaluating Expressions l Arithmetic Expression int result, a = 3, b = 5; result = ((a + b) / 2) % 3; ((a + b) / 2) % 3 (8 / 2) % 3 4 % 3 1
13
13 Assignment Statement variable = expression; l The value of constants / variables / expression in the right side of the = operator, is assigned to the variable in the left side of the = operator. l Examples: a = 5; b = a; c = a + b; l The parameter in the left hand side of the = operator should be a variable. l It can not be a constant or an expression. l Example of an invalid assignment statement : a + b = c ;
14
14 Assignment Statement l Java allows multiple assignment. int start, end; int width = 100, height = 45, length = 12; start = end = 0; l Note that whole integers appearing in the source code are taken to be ‘int’. So, one might wish to flag them when assigning to non-ints: float maxGrade = 100f ; // now holds ‘100.0’ double temp = 583d ; // holds double precision float temp = 5.5 ; // ERROR! // Java treats 5.5 as a double to // retain maximal accuracy l Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but we should prefer L): float maxGrade = 100F ; // now holds ‘100.0’ long x = 583l ; // holds 583, but looks like 5,381 long y = 583L ; // This looks much better!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.