You are on page 1of 13

BITS Pilani

Pilani Campus

Object-Oriented Programming (CS F213)

Lecture -2 [Introduction to Java]


JavaTM Technology

• Characterized by the  Each of these keywords are


following keywords very well explained in “Java
 Simple Language Environment”, a
 Architecture Neutral white paper written by James
 Objected Oriented
Gosling and Henry McGilton
 Portable
 Distributed
 Multithreaded
 High Performance
 Robust
 Dynamic
 Secure

2
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
3
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.

4
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  The Java platform has two
described as a components:
combination of the ◦ The Java Virtual Machine
operating system and ◦ The Java Application Programming
underlying hardware Interface (API)
• The Java platform differs  The API is a large collection of ready-
from most other platforms made software components that
in that it's a software-only provide many useful capabilities. It is
platform that runs on top grouped into libraries of related classes
of other hardware-based and interfaces; these libraries are
platforms.
known as packages 5
BITS Pilani, Pilani Campus
Creating Your First Application

• Checklist
– The Java SE Development Kit
– A text editor

 Creating application using simple text


editor
 Install JDK
 Check for correct installation
 Create HelloWorldApp.java
 Compile HelloWorldApp.java
 Run HelloWorldApp

 Creating application using Eclipse

6
BITS Pilani, Pilani Campus
A Sample Java Program

7
BITS Pilani, Pilani Campus
System.out.println

• Java programs work by having things called


objects perform actions
– System.out: an object used for sending output
to the screen
• The actions performed by an object are called
methods
– println: the method or action that the
System.out object performs

8
BITS Pilani, Pilani Campus
System.out.println

• Invoking or calling a method: When an object performs


an action using a method
– Also called sending a message to the object
– Method invocation syntax (in order): an object, a dot (period),
the method name, and a pair of parentheses
– Arguments: Zero or more pieces of information needed by the
method that are placed inside the parentheses

System.out.println("This is an argument");

9
BITS Pilani, Pilani Campus
class Bicycle {
int cadence = 0; The Bicycle
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
10
BITS Pilani, Pilani Campus
Running Bicycle
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();
} 11
} BITS Pilani, Pilani Campus
Questions
1. 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. /* comment
d. // comment
Answer: 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

12
BITS Pilani, Pilani Campus
THANK YOU

BITS Pilani, Pilani Campus

You might also like