Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java.

Similar presentations


Presentation on theme: "Introduction to Java."— Presentation transcript:

1 Introduction to Java

2 Java Java is a programming language originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. It is intended to let application developers "write once, run anywhere". Platform Independent

3 Characteristics of Java
Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic

4 JDK The Java Development Kit (JDK) is an implementation of either one of the Java platforms released by Oracle Corporation in the form of a binary product aimed at Java developers on Solaris,Linux, Mac OS X or Windows. The JDK includes a private JVM and a few other resources to finish the recipe to a Java Application.

5 JDK Versions JDK 1.02 (1995) JDK 1.1 (1996) JDK 1.2 (1998)
JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 JDK 1.7 (2011) a. k. a. JDK 7 or Java 7 JDK 1.8 (2014) a. k. a. JDK 8 or Java 8

6 JDK Editions Java Standard Edition (J2SE)
With the advent of Java 2 (released initially as J2SE 1.2 in December 1998–1999), new versions had multiple configurations built for different types of platforms Java Standard Edition (J2SE) J2SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets, Java ServerPages, and Java ServerFaces. Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile devices such as cell phones.

7 A Simple Java Program // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) System.out.println("Welcome to Java!"); }

8 Creating, Compiling, and Running Programs

9

10 JVM JVM-A Java virtual machine (JVM) is an abstract computing machine or virtual machine, JVM is a platform-independent execution environment that converts Java bytecode into machine language and executes it. Java is both compiler & interpreter based language.

11 Java ByteCode Java bytecode is the instruction set of the Java virtual machine. java compiler(javac) compiles java code to bytecodes and java interpreter(java) interpretes these bytecodes(line by line), convert it into machine language and execute.

12 Java IDE’s Eclipse Netbeans BlueJ JCreator MyEclipse Jdeveloper
Note : Use of javac /java at command prompt

13 Develop and run the Java program without IDE
Name of the file must be the same as the name of the class with .java, e.g. Welcome.java Convention: class name starts with capital letter To compile javac Welcome.java Generates a class file called Welcome.class To run java Welcome

14 Develop and run Java programs using IDE (NetBeans)
incorporates the Java compiler, the Java interpreter and other tools together with file and project management for developing and executing Java programs.  DEMO

15 Program Structure

16 Data Types

17

18 Selection Statements if Statements switch Statements

19 if Statements Example: if (booleanExpression) { statement(s); }
if ((i > 0) && (i < 10)) { System.out.println("i is an " + "integer between 0 and 10");

20 The if...else Statement if (booleanExpression) {
statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case;

21 if...else Example if (radius >= 0) { area = radius*radius*PI;
System.out.println("The area for the “ + “circle of radius " + radius + " is " + area); } else { System.out.println("Negative input");

22 Multiple Alternative if Statements
if (score >= 90) grade = ‘A’; else if (score >= 80) grade = ‘B’; if (score >= 70) grade = ‘C’; if (score >= 60) grade = ‘D’; grade = ‘F’; if (score >= 90) grade = ‘A’; else if (score >= 80) grade = ‘B’; else if (score >= 70) grade = ‘C’; else if (score >= 60) grade = ‘D’; else grade = ‘F’;

23 switch Statements switch (year) { case 7: annualInterestRate = 7.25;
break; case 15: annualInterestRate = 8.50; case 30: annualInterestRate = 9.0; default: System.out.println( "Wrong number of years, enter 7, 15, or 30"); }

24 switch Statement Rules
The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. (The case statements are executed in sequential order.) The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed.

25 switch Statement Rules, cont.
The default case, which is optional, can be used to perform actions when none of the specified cases is true. ·       The order of the cases (including the default case) does not matter. However, it is a good programming style to follow the logical sequence of the cases and place the default case at the end.

26 Repetitions while Loops do-while Loops for Loops break and continue

27 while Loop Flow Chart while (continuation-condition) { // loop-body; }

28 while Loop Flow Chart, cont.
int i = 0; while (i < 100) { System.out.println( "Welcome to Java!"); i++; }

29 do-while Loop do { // Loop body; } while (continue-condition);

30 for Loops for (initial-action; loop-continuation-condition; action-after-each-iteration) { //loop body; } int i = 0; while (i < 100) { System.out.println("Welcome to Java! ” + i); i++; Example: int i; for (i = 0; i < 100; i++) {

31 for Loop Flow Chart for (initial-action; loop-continuation-condition;
action-after-each-iteration) { //loop body; }

32 The break Keyword

33 The continue Keyword

34 Classes and Objects

35 Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area()

36 Data Abstraction Declare the Circle class, have created a new data type – Data Abstraction Can define variables (objects) of that type: Circle aCircle; Circle bCircle;

37 Creating objects of a class
aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle;

38 Constructors A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.

39 Constructors Constructors look a little strange because they have no return type, not even void. This is because the implicit return type of a class’ constructor is the class type itself. It is the constructor’s job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.

40 Parameterized Constructors
DEMO

41 Method Overloading

42 Overloading Method In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java supports polymorphism.

43 Overloading Method When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.

44 Overloading Method DEMO

45 Constructor Overloading
class Student5{ int id; String name; int age; Student5(int i,String n){ id = i; name = n; } Student5(int i,String n,int a){ age=a;

46 Access Specifiers

47 HeadStart Java Programming Course
Access Specifiers Specifies who may access variables. Also applies to classes, constructors, and methods. public available everywhere protected available only to the current class and its subclasses private available only to the class in which it is declared. This is applied at the class not the object level. default If no access specifier is explicitly, available only within the current package ©Copyright IBM Corp. 1996 Course materials may not be reproduced in whole or in part without the prior written permission of IBM Corp. or Footprint Software Inc.

48 private class A{ private int data=40;
private void msg(){System.out.println("Hello java");}   }      public class Simple{    public static void main(String args[]){      A obj=new A();      System.out.println(obj.data);//Compile Time Error      obj.msg();//Compile Time Error      }  

49 Private constructor If you make any class constructor private, you cannot create the instance of that class from outside the class. For example: class A{   private A(){}//private constructor   void msg(){System.out.println("Hello java");}   }   public class Simple{    public static void main(String args[]){      A obj=new A();//Compile Time Error    }  

50 public public class A{ public void msg(){System.out.println("Hello");} } class B{ public static void main(String args[]){ A obj = new A(); obj.msg(); } }

51 DEMO Public & private access specifiers Note :
protected access specifier is related to inheritance concepts Default access specifier is related to package concepts


Download ppt "Introduction to Java."

Similar presentations


Ads by Google