SESSION 1 Introduction in Java
Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments Variables and Datatypes Overview Netbeans
Objects Object is the representation of real world entities Example: persons, tables, computers… Each object has Characteristics to describe properties Actions Example, properties and actions of car
Software objects Similar to objects in the real world Software object also has: The sate of object is called attributes or fields The action of object is called behavior or method
Class Class is the template that defines the outline of states and behaviors for all objects belonging to that class Class is the concept model of entities Object is the instance of class Fields and method are the members of class
Introduction to Java Java being secure, portable, and platform independent was found to be capable of addressing large scale problems across the Internet Introduced in 1995 by Sun Microsystems Its objective was to develop a software for embedding in consumer electronic devices. Initially called ‘Oak’ Internet users had problems of portability and platform independence
Features of Java Object – oriented Compiled and interpreted Portable - Platform independent Robust Secure Distributed Multithread
Java technologies J2SE – java standar edition Console application Desktop application J2EE – java enterprise edition Servlet, JSP EJB, JMS Struts … J2ME – java micro edition
Java platform Platform is a hardware or software in which a program runs Ex: Windows, Linux, Solaris Java platform consists 2 essential components
Java Virtual Machine (JVM) JVM is the heart of Java JVM has an interpreter component that enables communication between Java byte code and a computer’s operating system Java code can run on any platform by using JVM JVM normally reads and executes Java statements one at a time Functionalities of JVM Loading.class file Managing the memory Performing the garbage collection
Java Development Kit (JDK) A set of command line tools Three major releases are: Java 1.0 – first release, 1996 Java 1.1 – 1997 release Java Java – 2002 Java 1.5 – 2004 Java 1.6 Contains the software and tools needed to compile, debug and execute applets and applications
Tools under JDK javac: compiler used to compile Java source code Syntax: javac [option] source Source files ends with an extension of.java Ex: javac Test.java java: interpreter used to execute Java byte codes java [option] classname [arguments] Ex: java Test javadoc javap
Configure JDK Path: specifies the location of javac, java, … tools Classpath: an environment variable that specifies thee location of class file and libraries needed for the Java compiler to compile the applications
A Sample Java program // This is a simple program called First.java class First { public static void main (String [] args) { System.out.println ("My first program in Java "); } Comments: //comment for one line /* …comment for multi lines */ /** …comments are generated in java document */
Compiling and executing the Java program The java compiler creates a file called 'First.class' that contains the byte codes
Compiling and executing the Java program (cont)
Variables Location in the merory where a value is stored and can be retrieved later Basic units of storeage in Java program Variables must be declared beforing using Declaring variable datatype variable; Example: int a;
Example class DynVar { public static void main(String [] args) { double len = 5.0, wide = 7.0; double num = Math.sqrt(len * len + wide * wide); System.out.println("Value of num after dynamic initialization is " + num); }
Name convention of variable Must begin a letter, $ or _ Must not be a keyword or reserved word Case-sensitive Not including white space If name consists of more than word, the first letter of each subsequent word should be capitalized Example: customerName my_phone $product_Code
Literals Fixed values Represented directly in code without requiring computeation Integer literals – represent an int value Decimal values – base 10 Octal value – base 8 Hexadecimal value – base 16 Float literal – represent decimal values with a fractional component. It has parts: Whole number: 0, 1, 2, …, 9 Decimal point: 4.03 Exponent: E or e, e+10, E-02 Suffix: D, d, f, F
Literals (cont…) Boolean literal – represent true or false Character literal – represent character is enclosed in single quotes Ex: ‘a’, ‘!’ Null literal String literal – represent sequence of characters enclosed in double quotes Ex: “hello”, “12a”
Data Types byte char boolean short int long float double Array Class Interface
Scanner Locate in java.util package Allows the user to read many types of data Syntax: import java.util.Scanner; Scanner s = new Scanner(System.in);