basic java

9
BASIC JAVA

Upload: madaline-walker

Post on 31-Dec-2015

9 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: BASIC JAVA

BASIC JAVA

Page 2: BASIC JAVA

Hello World

Page 3: BASIC JAVA

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

Java Comments

// for the java one liner /* for a multi-line comment */ /** for a javadoc comment */

Page 5: BASIC JAVA

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

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

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

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

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