An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1
2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public static void main(string args[]) { System.out.println( Hello World ); } }
3 Hello World: Java and C S1: S2: S3: S4: S6: // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public static void main(string args[]) { System.out.println( Hello World ); } } /* helloworld.c: Hello World program */ #define <stdio.h> void main(int argc, char *argv[]) { printf( Hello World\n ); }
4 Program Processing Compilation # javac HelloWorld.java results in HelloWorld.class Execution # java HelloWorld Hello World
5 Closer Look at - Hello World The class has one method main() public static void main(string args[]) { System.out.println( Hello World ); } Command line input arguments are passed in the String array args[] e.g java HelloWorld John Jane args[0] John args[1] Jane
6 Closer Look at - Hello World import java.lang.*; Java allows grouping of related classes into a package. It allows different companies can develop different packages, may even have same class and method names, but they differ by package name: ibm.mathlib.* microsoft.mathlib.* Helps in managing name clash problems. Think of this package as library. import statement somewhat serves similar purpose as C s #include If you don t add import statement, then you need utilise fully qualified name. ibm.mathlib.sin() If you do import ibm.* then you can use mathlib.sin() instead.
7 Java imports java.lang.* by default So, You don't need to import java.lang.* That means, you can invoke services of java s lang package classes/entities, you don t need to use fully qualified names. We used System.out.println() instead of java.lang. System.out.println()
8 public static void main(string args[]) public: The keyword public is an access specifier that declares the main method as unprotected. static: It says this method belongs to the entire class and NOT a part of any objects of class. The main must always be declared static since the interpreter users this before any objects are created. void: The type modifier that states that main does not return any value.
9 System.out.println( Hello World ); java.lang.* All classes/items in lang package of java package. System is really the java.lang.system class. This class has a public static field called out which is an instance of the java.io.printstream class. So when we write System.out.println(), we are really invoking the println() method of the out field of the java.lang.system class.
10 Java Program Structure Documentation Section Package Statement Import Statements Interface Statements Class Declarations Main Method Class { }
11 Basic Data Types Types boolean either true or false char 16 bit Unicode 1.1 byte 8-bit integer (signed) short 16-bit integer (signed) int 32-bit integer (signed) long 64-bit integer (singed) float 32-bit floating point (IEEE 754-1985) double 64-bit floating point (IEEE 754-1985) String (class for manipulating strings) Java uses Unicode to represent characters internally
Elements of Java lexical values, chapter 2 (pages 37-39) White spaces: Java is a free-form language. There is no special rue for us to follow. Identifiers: Identifiers are used for class names, method names and variables names. They must not begin with a number. The following is correct: height, a2, $sign The following is incorrect: 2dco, yes/no Literals: A constant value is created by using literal such as 200 (integer), 12.3 (floating-point value), DCO (string) 12
Elements of Java lexical values Comments: // (single line) /* */ (block) /**.. */ (documentation for HTML format Separators: separators are used to terminate statements () parentheses {} braces [] brackets ; semicolon, comma. Period 13
Java Keywords there are 49 words abstract finally public assert 3 float return boolean for short break goto 1 static byte if strictfp 2 case implements super catch import switch char instanceof synchronized class int this const 1 interface throw continue long throws default native transient do new try double package void else private volatile extends protected while 14 2017/2/13 final
Data Types and Variables Integers Floating point Characters Booleans Variables Type conversion (integer to floating) 15
Integers 4 types Name Size (bits) bytes Range byte 8 1-128 to 127 short 16 2-32768 to 32767 int 32 4-2147483648 to 2147483647 long 64 8-922372036854775808 to 9223372036854775807 16
Example of byte, short, int and long 17
Example out of range, (byte) 18
Floating 2 types Type size (bits) range Double 64 4.9exp(-324) to 1.8 exp(308) float 32 1.4exp(-45) to 3.4 exp(38) 19
Example long 20
Example out of range 21
Characters Java uses two bytes to store characters (0 to 65536) The standard of ASCII ranges from 0 to 127 For example, X in ASCII is 88 22
ASCII Table in hexadecimal (16) 23
Example character 24
Character.isLetter(ch) return true or false 25
Character.isDigit(ch) return true or false 26
Booleans Java has a simple type called boolean. It has two values, true or false For example, a = 4, b = 5, (a >b) is true For example, (4 = 4) is true For example, (3 > 4) is false 27
Example of boolean 28
Escape sequence Escape sequence uses \u to stand for white space. They are 16-bit values. Sequence name value \n newline \u000a \t tab \u0009 \b backspace \u0008 \r carriage return \u000d \f form feed \u000c \ single quote 29
String Strings literals are similar to English. Examples are: hello how are you? three \blines \ This statement is quoted \ 30
Example of String and Escape Sequence 31
Variables In java, variables are basic storage. The basic format is type identifier [= value], identifier [= value]; Examples are: int a =10, b = 20, c=30; int a, b, c; double a = 2.3, b = 4.5, d= 6.7; char x = z ; 32
Type conversion Byte defines the value between - 127 to 128. If you assign a value that is larger than 128, you have to use short or integer. This needs conversion. 33
Example 34
Example 35
Summary Integers 4 types, byte, short, int and long Floating point 2 types, float and double Characters ASCII, X Booleans true or false String How are you? Variables u67 (correct), 67u (incorrect) Type conversion (integer to floating) 36