lec 02 [intro to java]

12
BITS Pilani Pilani Campus Object-Oriented Programming (BITS C342) Lecture -2 [Introduction to Java]

Upload: garapati-avinash

Post on 14-Feb-2017

145 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Lec 02 [intro to java]

BITS PilaniPilani Campus

Object-Oriented Programming (BITS C342)

Lecture -2 [Introduction to Java]

Page 2: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

JavaTM Technology

• Characterized by the following keywords Simple Architecture Neutral Objected Oriented Portable Distributed Multithreaded High Performance Robust Dynamic Secure

2

Each of these keywords are very well explained in “Java Language Environment”, a white paper written by James Gosling and Henry McGilton

Enthusiastic students are

referred to the following link

http://java.sun.com/docs/white/langenv

/BITS C342, Object Oriented Programming

Page 3: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

The JavaTM Programming Language

• All source code is first written in plain text files ending with the .java extension

• Those source files are then compiled into .class files by the javac compiler

• A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine (Java VM)

• The java launcher tool then runs your application with an instance of the Java Virtual Machine

3BITS C342, Object Oriented Programming

Page 4: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

The JavaTM Programming Language

• Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris TM Operating System (Solaris OS), Linux, or Mac OS

• Through the Java VM, the same application is capable of running on multiple platforms.

4BITS C342, Object Oriented Programming

Page 5: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

The Java Platform

• A platform is the hardware or software environment in which a program runs

• Microsoft Windows, Linux, Solaris OS, and Mac OS

• Most platforms can be described as a combination of the operating system and underlying hardware

• The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms.

5

The Java platform has two components: ◦ The Java Virtual Machine◦ The Java Application

Programming Interface (API) You've already been introduced to

the Java Virtual Machine; it's the base for the Java platform and is ported onto various hardware-based platforms

The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages BITS C342, Object Oriented Programming

Page 6: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

Creating Your First Application

• Checklist– The Java SE Development Kit 6 (JDK 6) – A text editor

6

Creating application using NetBeans

Creating application using Eclipse

Creating application using simple text editor

Install JDK Check for correct

installation Create

HelloWorldApp.java Compile

HelloWorldApp.java Run HelloWorldApp

Download & Install Tools Java SE Development Kit (JDK

6)http://www.oracle.com/technetwork/java/javase/downloads/index.html Download NetBeanshttp://www.oracle.com/technetwork/java/javase/downloads/index.html Download Eclipsehttp://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/heliossr1

BITS C342, Object Oriented Programming

Page 7: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

A closer look at HelloWorldApp

The HelloWorldApp class implements an application that simply prints "Hello World!" to standard output.

7

class HelloWorldApp { public static void main(String[] args) {

// Display the string. System.out.println("Hello

World!"); }

}

The HelloWorldApp class definition

The most basic form of a class definition is

class name { . . . }

Every application in Java should have a main() method

modifiers public and static can be written in either order

It is the entry point of your application Accepts a single argument which is an

array of elements of type String Mechanism through which the runtime

system passes information to your application

BITS C342, Object Oriented Programming

Page 8: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

Example – 1

/* Code 1-1 The TestGreeting.java Application */

1. public class TestGreeting {2. public static void main (String args[]) {3. Greeting helloGreeting = new Greeting();4. helloGreeting.greet();5. }6. }

/* Code 1-2 The Greeting.java class */

1. public class Greeting {2. public void greet() {3. System.out.println(“Hello Greeting”);4. }5. }

8BITS C342, Object Oriented Programming

Page 9: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

Example - 2class Bicycle { int cadence = 0; int speed = 0; int gear = 1;

void changeCadence(int newValue) { cadence = newValue;

}void changeGear(int newValue) {

gear = newValue; } void speedUp(int increment) {

speed = speed + increment; } void applyBrakes(int decrement) {

speed = speed - decrement; } void printStates() { System.out.println(“cadence:” +cadence+ “speed:” +speed+ “gear:” +gear); }

} // End of Bicycle

9BITS C342, Object Oriented Programming

Page 10: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

class BicycleDemo {

public static void main(String[] args) {

// Create two different Bicycle objects Bicycle bike1 = new Bicycle(); Bicycle bike2 = new Bicycle();

// Invoke methods on those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates();

bike2.changeCadence(50); bike2.speedUp(10); bike2.changeGear(2); bike2.changeCadence(40); bike2.speedUp(10); bike2.changeGear(3); bike2.printStates();

} }

Example - 2

10BITS C342, Object Oriented Programming

Page 11: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

Questions1. When you compile a program written in the Java programming language,

the compiler converts the human-readable source file into platform-independent

code that a Java Virtual Machine can understand. What is this platform-independent

code called?Answer: Bytecode

2. Which of the following is not a valid comment: a. /** comment */b. /* comment */c. /* commentd. // commentAnswer: C

3. What is the correct signature of the main method? Answer: public static void main(String[] args)

4. When declaring the main method, which modifier must come first, public or static?

Answer: They can be in either order, but the convention is public static

5. What parameters does the main method define? Answer: The main method defines a single parameter, usually named args,

whose type is an array of String objects

11BITS C342, Object Oriented Programming

Page 12: Lec 02 [intro to java]

BITS Pilani, Pilani Campus

THANK YOU1. The material presented in these slides is used only for academic

purpose2. Copyright © Oracle Sun Microsystems

BITS C342, Object Oriented Programming