100 likes | 193 Views
BASIC JAVA. Hello World. // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s = c + "ello <br>Worl"; int last = 'd'; s = s + (char)last; System.out.println(s); } }. Java Comments.
E N D
// 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); }}
Java Comments • // for the java one liner • /* for a multi-line • comment */ • /** for a javadoc comment • */
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
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
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
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
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());