1 / 9

BASIC JAVA

BASIC JAVA. Hello World. // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s = c + &quot;ello <br>Worl&quot;; int last = 'd'; s = s + (char)last; System.out.println(s); } }. Java Comments.

Download Presentation

BASIC JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BASIC JAVA

  2. Hello World

  3. // 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); }}

  4. Java Comments • // for the java one liner • /* for a multi-line • comment */ • /** for a javadoc comment • */

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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());

More Related