1 Java Basics TAWFIQ JAWHAR COEN 352 TUTORIAL
1 Java Basics TAWFIQ JAWHAR COEN 352 TUTORIAL 1 CONCORDIA UNIVERSITY
Everything is Classes Java is an object oriented language and the class is the basic unit of organization in the Java Virtual Machine Even the main function will require a class Every. java file contains a class and the name of the file is the name of the class in it There are no header files in Java programs are compiled to bytecode and not machine code like C and C++. Once the program to bytecode it can run on any system that has JVM. Unlike C and C++ code that has to be recompiled for every targeted system. Functions are referred to as methods in Java. Methods are only declared as members of a class. 2
Hello. World. java public class Hello. World { public static void main(String[] args) { // Prints "Hello, World" to the terminal window. System. out. println("Hello, World"); } } 3
4 Primitive data types all have fixed size Category Integer Floating-point Other Types Size (bits) Minimum Value Maximum Value Precision Example byte 8 -128 127 From +127 to -128 byte b = 65; char 16 0 216 -1 All Unicode characters[1] char c = 'A'; char c = 65; short 16 -215 215 -1 From +32, 767 to -32, 768 short s = 65; int 32 -231 231 -1 From +2, 147, 483, 647 to -2, 147, 483, 648 int i = 65; long 64 -263 263 -1 From +9, 223, 372, 036, 854, 775, 807 to 9, 223, 372, 036, 854, 775, 808 long l = 65 L; float 32 2 -149 (2 -2 -23)· 2127 From 3. 402, 823, 5 E+38 to 1. 4 E-45 float f = 65 f; double 64 2 -1074 (2 -2 -52)· 21023 From 1. 797, 693, 134, 862, 315, 7 E+308 to 4. 9 E-324 double d = 65. 55; boolean -- -- -- false, true boolean b = true; void -- -- --
Garbage Collection The java platform uses garbage collector to automatically reclaim memory by recycling objects when they are no longer referenced. Unlike C++ where you have to delete the allocated memory to avoid memory leaks. 5
Classes 6 Class members are declared with access modifiers which specify the accessibility of the member outside of the class. Public or private modifiers have to be specified at every declaration. public class Clock { private long time = 0; public long get. Time() { return this. time; } public void set. Time(long the. Time) { this. time = the. Time; } }
All objects are references public class Equal. String. Example { public static void main(String[] args) { String s 1 = new String("TJ"); String s 2 = new String("TJ"); String s 3 = s 1; //references of the same object System. out. println("s 1 == s 2: "+(s 1 == s 2)); System. out. println("s 1. equals(s 2): "+(s 1. equals(s 2))); System. out. println("s 1 == s 3: "+(s 1 == s 3)); System. out. println("s 1. equals(s 3): "+(s 1. equals(s 3))); } } Output: s 1 == s 2: false s 1. equals(s 2): true s 1 == s 3: true s 1. equals(s 3): true 7
Java is all pass by value Excellent explanation: https: //stackoverflow. com/questions/40480/is-java-pass-by-reference-orpass-by-value 8
Inheritance 9 Java uses keyword extend to inherit from a class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System. out. println("Programmer salary is: "+p. salary); System. out. println("Bonus of Programmer is: "+p. bonus); } } Output: Programmer salary is: 40000. 0 Bonus of programmer is: 10000
Reference (and excellent source to learn more) https: //www. seas. upenn. edu/~cis 1 xx/resources/Java. For. Cpp. Programmers/j -javac-cpp-ltr. pdf 10
- Slides: 10