Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Programming Lesson 2 Lesson 2 Variables, Expression Java identifier Java identifier is a name consisting of letters and digits, the first character must be a letter, and you can use _, $. Most Java programmers don’t use underscores, instead initiate each interior word with an upper case character. They also start class name with uppercase and methods with small letters. (speed_of_light, SpeedOfLight). Java data Types 1- Numeric Integer Real 2- Non-numeric byte: 1 byte: -128 to 127 short: 2 byte int: 4 byte long: 8 byte float: 4 bytes double: 8 byte char: 2 byte String boolean Note: Java defines the exact sizes of its simple data type and these sizes are guaranteed to be the same on any computer that can run Java byte code. Assignment Assignment operator in Java is = To declare a variable: Syntax: Data-type variable-list separated by comma and may have initial values; Ex: int a, b, c =10, d; A compilation error may be generated If you use any variable before initialization. Comments // For single line /* For multiple lines *// Print statement To print any thing on the screen, use System.out.println. Ex: System.out.println (“ the value of a =”); System.out.println ( a ) ; or System.out.println (“ the value of a =”+ a); 4 Java Programming Lesson 2 Note: println method can take only one argument. In order to print more than one argument use + operator to concate the argument’s values. Java literal Boolean literal: true, false. String literal: “” Character literal: ‘ ‘. char c1 = ‘A’, c2 = 65; Numerical literal: Integer int (default) long (…l) Real double (default, …d) float (…f) Note: Hexadecimal values start with 0x. Octal values starts with 0. Ex: 0x10L. Real could have dot representation or scientific representation. Controls code: \n: new line \t: tab \\: backslash \’: single quote \”: double quote \nnn: char with nnn Unicode in octal system. \unnnn : char with nnnn Unicode represented in hexadecimal code, Ex: char c = ‘\u0037’ Assignments Rules: 1. Var1 = var2; Var1 must be the same type as var2 or greater (ex: int = int or short or byte;) 2. Var1 = literal; For int values: as long as the values are in the proper range, Java can convert them to the desired type. Ex: (b = 10; ok. b = 200 is not ok). For long literal: can be assigned only to long or float or double. Even if they are two small. Ex: (i = 1L is not ok). for float: float = float literal For double: double = double or float literal. Read Statements import java.io.*; // at the beginning of the file. public class Class1 {public static void main (String[] args) throws Exception { BufferedReader var = new BufferedReader ( new InputStreamReader(System.in)); String s = var.readLine(); Int I = Integer.parseInt(I); }} 5 Java Programming Lesson 2 Operators: +, -, *, /, %, ++, --, op = Integer Division: int / int Modules: %: int % int: return the remainder. While Real % Real return the remainder fraction. Ex: double d1 =7.6, d2 = 7 ; d2 = d1 % d2 ; // d2 = 0.6 Increment Operator: Ex: int j ,i = 100; j = i ++; // j = 100 , i=101 j = ++i; // j = 101 , i= 102 Ex: int i = 255; System.out.println (i++ +”” + ++i); // 255 257 Note: any expression returns a value (System.out.println (b=3); // return 3). Op = Var1 = var1 op var2 is equivalent to var1 op= var2 Ex: count = count +k; is equivalent to count += k; Ex : double R ; R =7.6 ; System.out.println (++R); // 8.6 Assignment: Rule: The destination should be as the large as the largest operand in order to hold the result. Ex: float = int + byte + float // or double Rule: if any value in integer expression is of type long then the result is long. Else the result is always int. Ex: byte b1= 1 , b2 = 2 ,b3; b3 = b1 + b2 ; // not ok Type casting If you want to tell Java to convert value from one type to another type use casting (prefix the value or the expression with the new type) . Ex: long l; int i; l = 10 ; // ok i = l ; // not ok i = (int) l; // it’s now your responsibility to ensure the validation. Ex: byte b1 =1 , b2 =2 ,b3; b3 = b1 + b2 ; // not correct 6 Java Programming Lesson 2 b3 = (byte) (b1+b2) ; // correct Ex: int R; double v = 4.99; R = (int) v; // R = 4 truncated 123456789- Precedence Rule < abbreviated > (), [ ] , dot ! , ++, --, new *, /, % +, < ,<= ,> , >= == , != && || =, op= Q: what is the value of x and y in the following expression: x = y =5; 7