Download presentation
Presentation is loading. Please wait.
Published byHester Barber Modified over 8 years ago
1
Primitive data types Lecture 03
2
Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public static void main(String args[]) { // Following code shows the multiplication table of 5. System.out.println("5 x 1 = 5 "); System.out.println("5 x 2 = 10"); System.out.println("5 x 3 = 15 "); System.out.println("5 x 4 = 20"); System.out.println("5 x 5 = 25"); System.out.println("5 x 6 = 30 "); System.out.println("5 x 7 = 35"); System.out.println("5 x 8 = 40 "); System.out.println("5 x 9 = 45"); System.out.println("5 x 10 = 50"); }
3
Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments Arithmetic operators
4
Example 123 (int) 1.5 (double) “HelloWorld” (String) `H’ (Char) ….
5
Basic Definitions Variables - A name that refers to a value. Assignment Statement - Associate a value with a variable. Constant: – 456—a literal numerical constant System.out.println(456); // Java – “A Literal String Constant” System.out.println(“My First Java”); // Java
6
There are eight primitive data types Boolean, byte, char, int, double, float, long, short In bytes, how long is the short data type? The int data type, the long data type? In bytes, how long is the float data type? The double data type? How long is the char data type?
7
Primitives Data types and Ranges PRIMITIVESIZE IN BITSRANGE int32bits (4 bytes)-2 31 to 2 31 -1 -2,147,483,648 to +2,147,483,647 long64bits -- 8 bytes-2 63 to 2 63 - 1 float32bits- -4 bytes-+(1.40129846432481707e-45 to 3.40282346638528860e+38} double64+-(4.94065645841246544e-324 to 1.79769313486231570e+308) char16bitsOne character string16bits per char Not applicable boolean8bits--1-byteTrue or false
8
Examples TypeSet of valuesSample literal vlues intinterges99 (-12) 214748647 doubleFloating-point numbers3.14 (-1.5) 6.0021 10 23 booleanBoolean valuesTrue or false charcharacters‘a’ ‘1’ ‘£’ ‘%’ ‘\n’ StringSequence of characters“AC” ”Hello” ” 1.5”
9
Variable declaration declarationVariable nameVariable type int x;xinteger double d;ddouble char c;ccharacter String s;sstring Float f;ffloat
10
The assignment operator = declarationVariable name int x; x = 36; Declare the variable x as an integer Sets x to constant 36 at execution time int x = 36; Sets x = to the constant 36 at compile time Initializes x to 36 at the time memory is set aside for it String y; y = “Hellow”; Declare the variable x as an integer Sets y to constant “Hello” at execution time String y = “Hello”; Sets y = to the constant “Hello” at compile time Initializes x to “Hello” at the time memory is set aside for it
11
Initialisation If no value is assigned prior to use, then the compiler will give an error Java sets primitive variables to zero or false in the case of a boolean variable All object references are initially set to null An array of anything is an object – Set to null on declaration – Elements to zero false or null on creation
12
Declaration Examples int index = 1.2; // compiler error boolean retOk = 1;// compiler error double fiveFourths = 5 / 4; // no error! float ratio = 5.8f;// correct double fiveFourths = 5.0 / 4.0;// correct 1.2f is a float value accurate to 7 decimal places. 1.2 is a double value accurate to 15 decimal places.
13
int a, b, c ; b =1; a=b; c =a; System.out.print(“c= “ + c); What is the value of a, b & c Declaration (Cont)
14
Example Int1.java // uninitialised data // this program will declare and print a number Public class int3 { public static void main(String[] arguments) { int weight; System.out.println("your weight is " + weight); } //end of program
15
Example Int2.java // this program will declare and print a number class int2 { public static void main(String[] arguments) { int weight = 68; System.out.println("your weight is " + weight); } //end of program
16
Example Int5.java // uninitialised data // this program will declare and print a number class int5 { public static void main(String[] arguments) { int weight; weight = 65 ; //65 = weight ; System.out.println("your weight is " + weight); } //end of program
17
Example String2.java // this program will declare and print a string class string2 { public static void main(String[] arguments) { String name = "Lahcen"; String x = "my name is "; System.out.println( x + name ); //print string x and then string name } //end of program
18
Basic Mathematical Operators * / % + - are the mathematical operators * / % have a higher precedence than + or - double myVal = a + b % d – c * d / b; Is the same as: double myVal = (a + (b % d)) – ((c * d) / b);
19
Basic arithmetic Operators OperatorMeaningExample +Addition2+3 = 5 *Multiplication2*3=6 -subtraction3-2=1 /division4/2=2 %mod5 % 2 = 1, 6 % 2= 0
20
Precedence Rules 1.Evaluate all sub-expressions in parentheses 2.Evaluate nested parentheses from the inside out 3.In the absence of parentheses or within parentheses a.Evaluate *, /, or % before + or – b.Evaluate sequences of *, /, and % operators from left to right c.Evaluate sequences of + and – operators from left to right
21
Built-in Math Functions in Java
22
Example: Calculate square root class SquareRootExp{ public static void main(String args[]) { int x = 25; int result = Math.sqrt(x); System.out.println(“Square root of " + x + “ is " + result); }
23
Exercise Assume that the length and width of the rectangle is 10 and 5. Write a program which compute area of the rectangle and display result on the screen.
24
Summary Different data type Declarations Arithmetic operators Parse string to integer. Boolean operators Assignments
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.