Chapter 2 Java
Chapter 2 Java
Chapter 2 Java
Introduction to
Java Programming
1
Introduction to Java programming
Java
write once, run anywhere
import javax.swing.JApplet; 6
to tell the compiler to load the JApplet class from the
javax.swing package
CONT’D
Java programs consist of pieces called classes.
Classes consist of pieces called methods that perform
tasks and return information when they complete their
tasks.
You can program each piece you may need to form a
Java program.
However, most Java programmers take advantage of
rich collections of existing classes in Java class libraries.
The class libraries are also known as the Java APIs.
Thus, there are really two pieces to learning the Java
“world.”
The first is learning the Java language itself so that you can
program your own classes;
the second is learning how to use the classes in the
extensive Java class libraries. 7
Ex. Math.pow(x, y) calculates the value of x raised to
the yth power
PHASES OF A JAVA PROGRAM
8
CONT’D
9
CONT’D
Editor – To type your program into , a editor
could be used for this
Compiler – To convert your high language
bytecode at a time,
CONT’D
11
SIMPLE JAVA PROGRAM
Step 1 ) Copy the following code into a notepad.
class A {
public static void main(String args[])
{
System.out.println(“First Java Program”);
}
}
14
CONT’D…
Writing my second example
let’s look at a simple problem for computing the area
of a circle. How do we write a program for solving this
problem?
public class ComputeArea
{
public static void main(String[] args)
{
// The main method is the entry point where the program starts
when it is executed
double radius; // Declare radius
double area; // Declare area
radius = 20; // New value is radius
// Compute area
area = radius * radius * 3.14159;
System.out.println("The area for the circle of radius " 15 +
radius + " is " + area);
}
CONT’D
Class
It is keyword which is used to declare a class
Public:
It is called access modifier or spacifier
It is used to define accessibility of data members (variable and
member function(methods)).
Static
Used for memory management
Can be used is variable function and block.
Static members are belongs to class rather than object(can be
called directly by the class name).
Static variable function and block will be in the following section.
Void:
there is no value to be returned by the function.
Main():
The execution of any program starts from the main function.
String[] args:
It is command line argument that you can write anything in place 16
of args.
System.out: stands output object
JAVA PROGRAM PRIMITIVES/ BASICS
Java Comments
Java Statements and blocks
Java Identifiers
Java Keywords
Variables
System.out.println() vs. System.out.print()
Reference Variables vs. Primitive Variables
Primitive variables
are variables with primitive data types. They store data in
the actual memory location of where the variable is.
Reference variables
are variables that stores the address in the memory
location.
It points to another memory location of where the
actual data is
17
When you declare a variable of a certain class, you
are actually declaring a reference variable to the
JAVA COMMENT
It is additional expression about which you are doing.
Two kinds of comment
Single line comment (//)
Multiline comment
/*
---
------
-----
*/
Java statement and block
{
double radius;
double area;
radius = 20;
18
}
IDENTIFIERS
the names of things that appear in the program..
All identifiers must obey the following rules:
An identifier is a sequence of characters that
consists of letters, digits, underscores (_), and
dollar signs ($).
An identifier must start with a letter, an
underscore (_), or a dollar sign ($).
It cannot start with a digit.
An identifier cannot be a reserved word.
19
An identifier cannot be true, false, or null.
An identifier can be of any length.
JAVA KEYWORD
The word which is pre-defined in the library
Its functionality also pre-defined.
20
VARIABLES
variables are used to store values to be used later in a
program.
They are called variables because their values can be
changed.
In the program radius and area are variables of
double-precision.
Variables are for representing data of a certain type.
To use a variable, you declare it by telling the compiler
its name as well as what type of data it can store.
The variable declaration tells the compiler to allocate
appropriate memory space for the variable based on
its data type.
The syntax for declaring a variable is 21
datatype variable Name;
SYSTEM.OUT.PRINTLN()
VS.
SYSTEM.OUT.PRINT()
System.out.print()
Print the output with the same line
System.out.print("hellow world ");
System.out.print(“this is my first java program");
System.out.println()
Print the output with different line.
System.out.println("hellow world ");
System.out.println(“this is my first java program");
22
JAVA SCANNER
For Example:
24
import java.util.*;
public class ScannerExample {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.nextLine();
System.out.println("Name is: " + name);
in.close();
}
25
}
Data Types in Java
Data types specify the different sizes and values
that can be stored in the variable. There are two
types of data types in Java:
Primitive data types: The primitive data types
include boolean, char, byte, short, int, long, float
and double.
Non-primitive data types: The non-primitive
data types include
Classes
Interfaces
Arrays
26
CONT’D
Operators
Arithmetic operators
Relational operators
Logical operators
Operator Precedence
27
OPERATORS
Java Arithmetic and Increment/Decrement
operators
Java arithmetic operators are used to perform addition,
subtraction, multiplication, division Modulus, Increment,
Decrement.
They act as basic mathematical operations.
{
int a=10;
int b=5;
System.out.println(++a+b);//16
}
Java Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to
28
assign the value 10 to a variable called x:
{
int b=5;
OPERATORS…
int x = 5;
int y = 3;
System.out.println(x == y);
}
OPERATORS…
Java Logical Operators
Logical operators are used to determine the logic
between variables or values:
Logical and(&&), logical or(||) and logical not(!)
{
int x = 5;
System.out.println(x > 3 || x < 4);
/* returns true because one of the conditions
are true (5 is greater than 3, but 5 is not less
than 4)
*/
}
30
OPERATORS…
Conditional Operator (?:)
The Java Conditional Operator selects one of two
expressions for evaluation, which is based on the value of
the first operands.
It is also called ternary operator because it
takes three arguments.
{
int x, y;
x = 20;
y = (x == 1) ? 61: 90;
System.out.println("Value of y is: " + y);
// Value of y is:90
y = (x == 20) ? 61: 90;
System.out.println("Value of y is: " + y);
// Value of y is:90
} 31
Operator Precedence
*, /, %, (+ or – from left to right).
OPERATORS…
Getting Input from the Keyboard
Java Scanner class allows the user to take input
from the console.
It belongs to java.util package.
It is used to read the input of primitive types like int,
double, long, short, float, and byte.
It is the easiest way to read input in Java program.
The java.util package should be import while using
Scanner class.
Syntax
Scanner sc=new Scanner(System.in);
Methods of Java Scanner Class
Java Scanner class provides the following methods to 32
read different primitives types:
OPERATORS…
Method Description
Branching Statements
break statement
continue statement
return statement
35
.
CONTROL STRUCTURES/IF/
Use the if statement to specify a bloc of Java code to be
executed if a condition is true
Syntax:
if (condition)
{ // block of code to be executed if the condition is true
}
Example:
if (20 > 18)
{
System.out.println("20 is greater than 18");
}
Self test
Write a java program that will display “young” if your age is less
36
than 20.
}
switch(expression)
{
case x: // code block
break;
case y: // code block
break;
39
default: // code block }
CONT’D
int day = 4;
switch (day)
{
case 1: System.out.println("Monday");
break;
case 2: System.out.println("Tuesday");
break;
case 3: System.out.println("Wednesday");
break;
case 4: System.out.println("Thursday");
break;
case 5: System.out.println("Friday");
break;
case 6: System.out.println("Saturday");
break;
40
default:
System.out.println("Sunday");
REPETITION CONTROL STRUCTURES/WHILE LOOP/
Loops can execute a block of code as long as a specified condition
is reached.
Loops are handy because they save time, reduce errors, and they
make code more readable.
The while loop loops through a block of code as long as a
specified condition is true:
Syntax
while (condition)
{ // code block to be executed
}
Example:
int i = 0;
while (i < 5)
{
System.out.println(i); 41
i++;
}
REPETITION CONTROL
STRUCTURES/FOR LOOP/
When you know exactly how many times you want to
loop through a block of code, use the for loop instead of
a while loop
Syntax
for (statement 1; statement 2; statement3)
{
// code block to be executed
}
Statement 1 is executed (one time) before the execution of
the code block.
Statement 2 defines the condition for executing the code
block.
Statement 3 is executed (every time) after the code block
has been executed.
Example: 42
for (int i = 0; i < 5; i++)
{ System.out.println(i);
}
JAVA APPLICATION
Is a computer program that executes when
you use the java command to launch the Java
Virtual Machine (JVM)
The application program interface (API)
contains predefined classes and interfaces
for developing Java programs.
Java is a full-fledged and powerful language
46