Introduction to Java Programming Basics
Learn about the fundamentals of Java programming, including classes, objects, primitive data types, and memory management. Understand the structure of Java programs, declaration of class members, and the use of access modifiers. Explore the concept of garbage collection and the role it plays in memory management in Java.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
1 Java Basics TAWFIQ JAWHAR COEN 352 TUTORIAL 1 CONCORDIA UNIVERSITY
Everything is Classes 2 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. 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.
HelloWorld.java 3 public class HelloWorld { public static void main(String[] args) { // Prints "Hello, World" to the terminal window. System.out.println("Hello, World"); } }
4 Primitive data types all have fixed size Size (bits) 8 Minimum Value -128 Maximum Value 127 Category Types Precision Example byte From +127 to -128 byte b = 65; char c = 'A'; char c = 65; short s = 65; int i = 65; char 16 0 216-1 All Unicode characters[1] Integer short int 16 32 -215 -231 215-1 231-1 From +32,767 to -32,768 From +2,147,483,647 to -2,147,483,648 From +9,223,372,036,854,775,807 to - 9,223,372,036,854,775,808 From 3.402,823,5 E+38 to 1.4 E-45 From 1.797,693,134,862,315,7 E+308 to 4.9 E-324double d = long 64 -263 263-1 long l = 65L; float 32 2-149 (2-2-23) 2127 float f = 65f; Floating-point double 64 2-1074 (2-2-52) 21023 65.55; boolean b = true; -- boolean -- -- -- false, true Other void -- -- -- --
Garbage Collection 5 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.
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 getTime() { return this.time; } public void setTime(long theTime) { this.time = theTime; } }
All objects are references 7 public class EqualStringExample { public static void main(String[] args) { String s1 = new String("TJ"); String s2 = new String("TJ"); String s3 = s1; //references of the same object System.out.println("s1 == s2: "+(s1 == s2)); System.out.println("s1.equals(s2): "+(s1.equals(s2))); System.out.println("s1 == s3: "+(s1 == s3)); System.out.println("s1.equals(s3): "+(s1.equals(s3))); } } Output: s1 == s2: false s1.equals(s2): true s1 == s3: true s1.equals(s3): true
Java is all pass by value 8 Excellent explanation: https://stackoverflow.com/questions/40480/is-java-pass-by- reference-or-pass-by-value
Inheritance 9 Java uses keyword extend to inherit from a class 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) 10 https://www.seas.upenn.edu/~cis1xx/resources/JavaForCppProgra mmers/j-javac-cpp-ltr.pdf