L7 - IEG4180 Tutorial 7 Java Basics Bosco, Fong Chi Hang...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
IEG4180 Tutorial 7 – Java Basics Bosco, Fong Chi Hang (Acknowledgement: some materials in this tutorial are adopted from previous works  by Shing and Zero.)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java Basic Write the program Filename extension: .java Filename should be the same as the public class name One public class one source file Compile and run the program javac HelloWorldApp.java   (you will have  HellpWorldApp.class ) java HelloWorldApp  (no extension)
Background image of page 2
Hello World Define class name (So filename is HelloWorldApp.java) public class HelloWorldApp { public static void main (String args[]){ System.out.println(“Hello World!”); } } Application entry point; similar to C
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Data Type Primitive Type int, boolean, short, char, etc. Reference Type All class object E.g. String, Integer, Socket, etc. int a; //a represent the integer itself a = 123; //just declare and use it Integer a; //a is only a reference pointer a = new Integer(123); //you must call new to create the object
Background image of page 4
Illustration Primitive Type Reference Type int a; a = 123; a = 456; null a 1000 123 1000 456 1010 1010 ? a 123 456 Integer a; a = new Integer(123); a = new Integer(456);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
String String variable1 =  Hello ; String variable2 = new String( Hello ); variable1 +=   World ; Convert String to int int a = Integer.parseInt( 10 ); Convert int to String String b = String.valueOf(10); String b =  “”  + 10;
Background image of page 6
Array Array of primitive   new once int[] arrayInt =  new  int[10]; // int arrayInt[] arrayInt[0] = 123; Array of reference   new twice ClassApple[] apples =  new  ClassApple[10]; for (int i=0; i<apples.lenght; i++) { apples[i] =  new  ClassApple(); }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Memory Management Java programmer never free allocated object  manually. JVM will reclaim unreferenced memory through  garbage collection . Garbage collection is performed automatically 
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

L7 - IEG4180 Tutorial 7 Java Basics Bosco, Fong Chi Hang...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online