JAVA Programming Language Syntax of JAVA (literal) (Variable and data types) (Comments) (Arithmetic) (Comparisons) (Operators) 2
HelloWorld application Helloworldjava // class HelloWorld { //attribute //methods // HelloWorld public static void main ( String args [ ] ) { /* public void main, String args[ ] main */ String msg = hello world! Systemoutprint( hello, world! ); Systemoutprintln(msg); // System out print // 3 HiJava applet HiJavajava // import javaawtgraphics; // import c #include import javaappletapplet; // javaawtgraphics public class HiJava extends javaappletapplet { javaawt /* javaappletapplet extends HiJava */ public void paint(graphics g) //paint { gdrawstring( Hi, my friend Java, 5, 25); ***html // drawstring x,y // <applet code = HiJavaclass width=250 height=250 > 4
class object Class template Object attribute + operation( + ),, 5 class object class Motorcycle { // Motorcyclejava String make; String color; boolean enginestate; public Motorcycle( ){ void startengine( ) { void showatts( ) { // attribute // construct // methods public static void main (String args [ ] ) { Motorcycle m = new Motorcycle( ); // Motorcycle new // m mmake = "Yamaha RZ350"; // m mcolor = "yellow"; Systemoutprintln("Calling showatts"); mshowatts( ); // class m Systemoutprintln("--------"); Systemoutprintln("Starting engine"); mstartengine( ); 6
(identifier) HelloWorld, heydude3, tail, pounda Hello World //, /t, /n 3heyDude // short // #pounda // # 7 abstract default if private throw boolean do implements protected throws break double import public transient byte else instanceof return try case extends int short void catch final interface static volatile char finally long super while class float native switch const for new synchronized continue goto package this 8
(literal),, : 8,10,16 12=10 =16 0xC ( 0x ) = 8014 ( 0 ) 32bit int 64bit long ( l ): long pennytottal = 4L; : float double 32bit float ( f ):float pivalue = 31415927f; 64bit double: double mygpa = 225; 9 (literal) (boolean) true false boolean tothineownself = true; Java C 1 true, 0 false, true true false false ( ) a, #, 3 10
(literal) \n \f \d 8 \t \\ \xd 16 \b \ \r \ \ud String class (String primitive type ) String password= Lucifer ; c, struct / union 11 (Variable) (= ), _, $ int highscore; String username; boolean gameover; int zipcode = 90210; String name = randon ; boolean cheatedonkelly = true; int age = 28, height = 70, weight = 140; 12
(separators) { ; :, 13 (Comments) /* */ // /** */ Javadoc HTML 14
/** * A TicTacToe applet A very simple, and mostly brain-dead * implementation of your favorite game! <p> * * In this game a position is represented by a white and black * bitmask A bit is set if a position is ocupied There are * 9 squares so there are 1<<9 possible positions for each * side An array of 1<<9 booleans is created, it marks * all the winning positions * * @version 12, 13 Oct 1995 * @author Arthur van Hoff * @modified 04/23/96 Jim Hagen : winning sounds * @modified 02/10/98 Mike McCloskey : added destroy() */ 15 Javadoc 16
Javadoc html 17 (Data types) (primitive types) (reference type) (Primitive type) : byte, short, int, char ( ) : float, double (reference type),, 18
(Data types) ( ) Primitive type Size Minimum Maximum Default value boolean 1-bit - - False char 16-bit Unicode 0 Unicode 2 16-1 \u0000 (null) byte 8-bit -128 +127 (byte)0 short 16-bit -32,768 +32,767 (short)0 int 32-bit -2,147,483,648 2,147,483,647 0 long 64-bit -2 63 2 63-1 0L float 32-bit IEEE754 IEEE754 00f double 64-bit IEEE754 IEEE754 00d void - - - 1 In Java version 11 only, not in 10 19 (reference type) 20
(reference type) class Car{ public static void main( ){ 1 int a = 100 ; 2 Car mycar, yourcar; 3 mycar = new Car(); 4 yourcar= mycar; 1 int a = 100; a, 100 2 Car mycar, yourcar; Car 3 mycar = new Car(); Car, (300) mycar 4 yourcar =mycar; yourcar mycar (300), yourcar mycar 21 (array) Simple or composite data types, int number[ ] ; char [ ] letters ; int grid [ ] [ ] ; char alphabet [ ] = new char [26] int primes = {7,11,13 22
Array Java ( : exception) String difficultwords[ ]; String[ ] difficultwords; Point hits[ ]; Point[ ] hits; int donations[ ]; int[ ] donations; 23 Array new [ 1] [ ] = new [ ]; [ 2] [ ] = new [ ]; ( ) String[ ] playernames = new String[10]; ( ) int temp[ ] = new int[99]; : 0 boolean : false : \0 : NULL (index) 0 = ( ) int[ ] arrayofints = ; // compile error int[ ] simple = {0, 1, 2, 3, 4; int simple[ ] = {0, 1, 2, 3, 4; 24
Array, String[] Speak = new String[10]; length length int len = Speaklength; = length - 1 len = 10 length ( ) 25 Array int[ ][ ] coords = new int[12][12]; coords[0][0] = 1; coords[0][1] = 2; new int[3][3][2] 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 2 0 1 2 0 1 2 0 1 2 26
(Operator) (Arithmetic operator) *, /(DIV), %(MOD), +(add), -(subtract) (Logical operator) &&(AND), (OR),!(NOT) (Relational operator) <, <=, ==,!=, >, >= instanceof( ) - Boolean - true - false 27 (Operator) (Unary operator) +(plus), -(minus), ++(increase), --(decrease) ~( ) (Bit operator) &(AND), (OR), ^(XOR), ~(NOT) (Conditional operator)? : ( )? (true) : (false) int a= 7, b = 10; int c = ((a * 2) < (a + b))? a : b; c a 7 28
(Operator) public class AutoInc { public static void main(string args[]) { int i = 1; prt("i : " + i); prt("++i : " + ++i); // Pre-increment prt("i++ : " + i++); // Post-increment prt("i : " + i); prt("--i : " + --i); // Pre-decrement prt("i-- : " + i--); // Post-decrement prt("i : " + i); static void prt(string s) { Systemoutprintln(s); ///:~ Output i : 1 ++i : 2 i++ : 2 i : 3 --i : 2 i-- : 2 i : 1 +, (String) (concatenate) 29 (Operator) (Shift operator) << ( ) n << s : n 2 s (n*2 s ) 0 >> ( ) n >> s : n 2 s (n/2 s ) >>> ( ) n >>> s n : n >> s n : n s 0 30
(Operator) (Cast operator) (Storage type) int a = 23; Systemoutprintln( Casting test ); long b = (long) ( a * 2323); 31 (typecasting) Byte Short Char Int Long Float Short, char, int, long, float, double int, long, float, double int, long, float, double long, float, double float, double double 32
(typecasting), (=),,, (promotion) '+' (concatenate) int j : int j = 10; Systemoutprintln("j = " + j); j 5 160 50 : int j = 5; double f = 160 + j; 33 (typecasting) : CastOnejava 1 class CastOne { 2 public static void main(string args[]) { 3 byte byte1 = 10; 4 short short1 ; 5 char char1 = 10; 6 int int1 ; 7 float float1 ; 8 long long1 ; 9 double double1 ; 10 11 short1 = byte1; // byte(8 bits) => short(16 bits) 12 int1 = short1; // short(16 bits) => int(32 bits) 13 int1 = char1; // char(16 bits) => int(32 bits) 14 long1 = int1; // int(32 bits) => long(64 bits) 15 float1 = int1; // int(32 bits) => float(32 bits) 16 double1 = long1; // long(64 bits) => double(64 bits) 17 18 34
(Operator) [] () ++ --! ~ instanceof new (type)expression * / % + - << >> >>> < > <= >= ==!= & ^ &&? : = += -= *= /= %= ^= &= = <<= >>= >>>= 35 (Operator) )) (+(plus), -(minus), ++(increase), --(decrease)~( (+, --, *, /, %, &,, ^, <<,>>,>>>) (<, >, <=, >=, ==,!=) (++, -- --,) (+, --, *, /, % ) (<, >, <=, >=, ==,!=) (&,, ^, &&,,!, ==,!=,? ) (+) 36
(Operator) (Assignment operator) =, +=, -=, *=, /=, %= <<=, >>=, >>>= &=, ^=, = 37 (Operator) == equals( ) == equals( ) ==, equals( ) public class Equivalence { public static void main(string args[]) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); Systemoutprintln(n1 == n2); Systemoutprintln(n1!= n2); Output false true ==,!= 38
(Operator) public class EqualsMethod { public static void main(string args[]) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); Systemoutprintln(n1equals(n2)); ///:~ Output true equals() 39 : Equaljava 1 public class Equal { 2 public static void main(string args[]) { 3 String a = new String("abc"); 4 String b = "abc"; 5 String c = b; 6 String d = a; 7 StringBuffere = newstringbuffer("abc"); 8 StringBufferg = e; 9 if(a == b) { 10 Systemoutprintln("a==b"); 11 // if(a == e) { // 12 // Systemoutprintln("a==e"); 13 if(c == b) { 14 Systemoutprintln("c==b"); 15 if(dequals(a)) { 16 Systemoutprintln("dequals(a)"); 17 if(dequals(b)) { 18 Systemoutprintln( dequals(b)"); 19 if(bequals(e)) { Systemoutprintln("bequals(e)"); % java Equal c==b dequals(a) dequals(b) 40
class ArithmeticTest { public static void main(string args[ ]) { Systemoutprintln("4+2 : " + (4+2)); Systemoutprintln("45-2 : " + (45-2)); Systemoutprintln("44*22 : " + (44*22)); Systemoutprintln("46/3 : " + (46/3)); Systemoutprintln("46%3 : " + (46%3)); Systemoutprintln("46%31 : " + (46%31)); C:\>java ArithmeticTest 4+2 : 6 45-2 : 25 44*22 : 9680000000000001 46/3 : 15333333333333332 46%3 : 15999999999999996 46%31 : 14999999999999996 41 class LogicalTest { public static void main(string args[ ]) { int x, y, z; boolean t; C:\>java LogicalTest x=2, y=1, z=1, t=true x=3, y=2, z=2, t=true x=0, y=-1, z=-1, t=false x=1, y=0, z=0, t=false x = y = z = 1; t = ((++x!= 0) (++y!= 0) (++z!= 0)); Systemoutprintln("x=" + x + ", y=" + y + ", z=" + z + ", t=" + t); t = ((++x!= 0) (++y!= 0) (++z!= 0)); Systemoutprintln("x=" + x + ", y=" + y + ", z=" + z + ", t=" + t); x = y = z = -1; t = ((++x!= 0) && (++y!= 0) && (++z!= 0)); Systemoutprintln("x=" + x + ", y=" + y + ", z=" + z + ", t=" + t); t = ((++x!= 0) & (++y!= 0) & (++z!= 0)); Systemoutprintln("x=" + x + ", y=" + y + ", z=" + z + ", t=" + t); 42
class IncDecTest { public static void main(string args[ ]) { int i=23, j=8, k; k = ++i + j--; Systemoutprintln("i=" + i + ", j=" + j +", k=" + k); k = i-- + --j; Systemoutprintln("i=" + i + ", j=" + j +", k=" + k); Systemoutprintln("i=" + ++i + ", j=" + j-- + ", i=" + --i + ", j=" + --j); C:\>java IncDecTest i=24, j=7, k=32 i=23, j=6, k=30 i=24, j=6, i=23, j=4 43 class RelationalTest { public static void main(string args[ ]) { int a=3, b=4; boolean c; c = a < b; Systemoutprintln("a=" + a + ", b=" + b + ", c=" + c); C:\>java RalationalTest a=3, b=4, c=true 44
class BitwiseTest { public static void main(string args[ ]) { byte x=(byte)0x0a, y=(byte)0x48, z=(byte)0xcc; Systemoutprintln(" x y & z = " + (byte)(x y & z)); Systemoutprintln(" x y & ~z = " + (byte)(x y & ~z)); Systemoutprintln(" x ^ y & ~z = " + (byte)(x ^ y & ~z)); x = (byte)1; y = (byte)-1; Systemoutprintln(" ~x x = " + (byte)(~x x)); Systemoutprintln(" x ^ x = " + (byte)( x ^ x)); Systemoutprintln(" x << 3 = " + (byte)(x << 3)); Systemoutprintln(" x >> 3 = " + (byte)(x >> 3)); Systemoutprintln(" x >>> 3 = " + (byte)(x >>> 3)); Systemoutprintln(" y << 3 = " + (byte)(y << 3)); Systemoutprintln(" y >> 3 = " + (byte)(y >> 3)); Systemoutprintln(" y >>> 3 = " + (byte)(y >>> 3)); 45 class AssignmentTest { public static void main(string args[ ]) { int x=2, y=5, z=0; x += 3 + 2; Systemoutprintln("x=" + x + ", y=" + y + ", z=" + z); x += y -= z = 4; Systemoutprintln("x=" + x + ", y=" + y + ", z=" + z); C:\>java AssignmentTest x=7, y=5, z=0 x=8, y=1, z=4 46
class ConditionalTest { public static voidmain(string args[ ]) { int a=23, b=12, max, min; min = (a < b)? a : b; Systemoutprintln("min=" + min + ", a=" + a + ", b=" + b); max = (a > b)? a : b; Systemoutprintln("max=" + max + ", a=" + a + ", b=" + b); C:\>java ConditionalTest min=12, a=23, b=12 max=23, a=23, b=12 47 class OperatorTest { public static void main(string args[ ]) { int a=10, b=10; float f = (float)3141592; Systemoutprintln("a=" + a + ", f=" + f + ", b=" + b); a = (int)f-- * --b; Systemoutprintln("a=" + a + ", f=" + f + ", b=" + b); C:\>java OperatorTest a=10, f=3141592, b=10 a=27, f=2141592, b=9 48