basic java
DESCRIPTION
BASIC JAVA. Hello World. // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s = c + "ello \nWorl"; int last = 'd'; s = s + (char)last; System.out.println(s); } }. Java Comments. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/1.jpg)
BASIC JAVA
![Page 2: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/2.jpg)
Hello World
![Page 3: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/3.jpg)
// Hello world programpublic class MyFirstJavaProgram {public static void main(String args[]) { char c = 'H'; String s = c + "ello \nWorl"; int last = 'd'; s = s + (char)last; System.out.println(s); }}
![Page 4: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/4.jpg)
Java Comments
// for the java one liner /* for a multi-line comment */ /** for a javadoc comment */
![Page 5: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/5.jpg)
Simple output
System.out.println(“Hello World”); System.out.print(“No new line”); System.out.print(6); System.out.print(true); System.out.println(6+10); displays 16 System.out.println(“”+6+10); displays 610
![Page 6: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/6.jpg)
Primitive Types
byte there are no byte literals 8 bits short there are no short literals 16 bits int literals(23, -98, 077, 0xAF) 32 bits long lierals(23L, -9L) 64 bits float literals (34F, -4.5f) 32 bits double literals (34.0, 5e2, .4) 64 bits char literals (‘A’, ‘\n’, ‘\377’, ‘\u0041’) 16
bits unsigned boolean literals (true, false) 16 bits
![Page 7: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/7.jpg)
Assignment and types
short m = 5; // int to short is ok if it fits short m = 500000; // compiler error 50000 int
to short not ok float m = 50.0; // compiler error need to
cast double to float float m = (float)50.0; // ok cast the double to float byte b = 4L; // compiler error cast to byte
![Page 8: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/8.jpg)
More on Assignment and types
double m = 6; // int to double is fine int m = 6.0; // compiler error need to cast
the double to int int m = (int)6.0; // works fine double c = ‘\n’; // ok but strange char c = ‘A’ + 1; // c now holds ‘B’ System.out.println(‘A’ + 1); // displays 66 System.out.println((char)(‘A’ + 1)); // displays B
![Page 9: BASIC JAVA](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812c90550346895d913d87/html5/thumbnails/9.jpg)
The String Type
String s = "Peanut"; // or, see below String s = new String(“Peanut”); // same as
above Use concatenation for long strings System.out.println("This is " +
"a very long string"); Many string operations exist System.out.println(s.length()); System.out.println("Hello".length());