Introduction to Java.

Slides:



Advertisements
Similar presentations
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Excerpts from Introduction to Java Programming, 4E Author: Y. Daniel Liang (Copyright by Prentice Hall)
Introduction to Java Programming, 4E
Introduction to Java Programming, 4E Y. Daniel Liang.
For more Lectures and Notes Visit
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
L EC. 01: J AVA FUNDAMENTALS Fall Java Programming.
01 Introduction to Java Technology. 2 Contents History of Java What is Java? Java Platforms Java Virtual Machine (JVM) Java Development Kit (JDK) Benefits.
LESSON 1 INTRODUCTION Compiled By: Edwin O. Okech [Tutor, Amoud University] JAVA PROGRAMMING.
Advanced OOP MCS-3 OOP BSCS-3 Lecture # 1
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Programming Languages Machine.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
CPCS  Problem Solving in Everyday Life  Types of Problems  Problem Solving with Computers  Difficulties with Problem Solving 0-3.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Programs,
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
1.  At the end of this slide, student can:  Explore tools, features, properties and interface of the Textpad.  Creating a new project.  Open and run.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Programs.
Introduction to Java Programming with Forte Y. Daniel Liang.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Introduction to Java Programming. Introduction Course Objectives Organization of the Book.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Application Architecture Using Java Hong Li. Introduction Developed by a team led by James Gosling at Sun Microsystem. Originally called Oak, designed.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
introductory lecture on java programming
Lecture1 Instructor: Amal Hussain ALshardy. Introduce students to the basics of writing software programs including variables, types, arrays, control.
1 Introduction to Java. 2 * 10% Assignments/ class participation * 10% Pop Quizzes * 05% Attendance * 25% Mid Term * 50% Final Term.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Introduction to JAVA Programming
JAVA TRAINING IN NOIDA. JAVA Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented and specifically.
Introduction to Java Programming, 4E Y. Daniel Liang.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Introduction to Control Statements IT108 George Mason University.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Introduction to Object Oriented
Object Oriented Programming in
Topic: Classes and Objects
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
GC101 Introduction to computer and program
Chapter 1 Introduction to Computers, Programs, and Java
Chapter No. : 1 Introduction to Java.
Selections Java.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Programming Language Concepts (CIS 635)
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 5 Control Statements
Advanced Programming Fall 2017.
Chapter 1 Introduction to Computers, Programs, and Java
Units with – James tedder
Units with – James tedder
(Computer fundamental Lab)
Java History, Editions, Version Features
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Introducing Java.
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
Presentation transcript:

Introduction to Java

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

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

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.

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

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.

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!"); }

Creating, Compiling, and Running Programs

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.

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.

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

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

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

Program Structure

Data Types

Selection Statements if Statements switch Statements

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

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

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");

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’;

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"); }

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.

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.

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

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

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

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

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++) {

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

The break Keyword

The continue Keyword

Classes and Objects

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

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;

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

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.

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.

Parameterized Constructors DEMO

Method Overloading

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.

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.

Overloading Method DEMO

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;

Access Specifiers

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.

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      }  

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    }  

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(); } }

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