You are on page 1of 50

Chapter 2

Introduction to
Java Language
Java Programming Language
• one of the most popular and widely used programming
languages written and developed by Dr. James Gosling and his
colleagues in early 1900 at Sun Microsystem, which is now
part of Oracle Corporation after it was completed on January
27, 2010
• released in 1995 as part of Sun Microsystem’s Java platform
• based primarily in terms of syntax and structure in C and C++
Brief History of Java
• James Gosling, Mike Sheridan, and Patrick Naughton initiated
the Java language project in June 1991.
• Originally designed for interactive television but it was too
advanced for the digital cable television industry at the time.
• Initially called oak after an oak tree that stood outside
Gosling’s office
Brief History of Java
• Sun Microsystems released the first public implementation as
Java 1.0 in 1995 and it promised “Write once, Run
Anywhere”(WORA)
• On November 13,2006,Sun released much of Java as free and
open-source software.
Types of Java Applications
1. Java Applet-usually stored on a website and is downloaded and run client
computer from within a web browser
2. Application- can run only on the computer.
3. JAR(Java Archive) File-is used to package java files together into a single file
4.Servlet-runs on a web browser and helps to display web pages
5. Swing application- used to build an application that has a GUI(windows,
buttons, menus, etc.)
6. EJB(Enterprise Java Beans)-runs on a web server and is used to develop large,
complex websites
Characteristics of Java
1. Java is simple
2. Java is object-oriented.
3. Java is distributed.
4. Java is interpreted.
5. Java is robust.
6. Java is secure.
7. Java is architecture- neutral
Characteristics of Java
8.Java is portable.
9. Java’s performance
10. Java is multithreaded.
11.Java is dynamic.
Benefits of Java
1. used to teach students how to program as a first language
2. Requires that each variable be initialized .
3. Requires that each method that declares a return type ,
always returns a value.
4. Java comes with a large set of classes and methods
Benefits of Java
5.Unlike C , Java primitive types, such as int, are always the
same size in the number of bits which helps achieve cross-
platform compatibility
6. Used to be thought of being slower than C, but less important
in recent years with computers being faster.
7. Java has exception handling that requires a programmer to
handle error-conditions such as input/output errors.
Benefits of Java
8. Code compiled on one Java platform can be run on other
platforms that support Java without modification of either
source code nor the byte-code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Java Syntax
• The syntax of the Java programming is the set of rules defining
how a Java program is written and interpreted.
• Syntax is mostly derived from C AND C++, Java is almost
exclusively an object-oriented language.
Variable Declaration
• Variable- is a value that can be changed depending on
conditions or on information passed to the program
• In Java programming, u can define a variable with the
following form:
Data Type Variable Name and Value
Ex. String User_Name=“ Miley Cyrus”
Rules in Using Variables
A variable can contain:
1. Any Unicode character that is a letter(including numeric
letters like Roman numerals) or a digit.
2. Currency sign(such as $)
3. Connecting punctuation character(such as _).
Rules in Using Variables
A variable cannot :
1.Variable names are case-sensitive.
2. A variable ‘s name can be any legal identifier.
3. It can contain unicode letter, digits and two special characters such
as underscore and $ dollar sign.
4. Length of variable name can be any number.
5. It is necessary to use alphabet at the start(however, we can use
underscore).
Rules in Using Variables
A variable cannot :
6. Some auto generated variables may contain ‘$’ sign. But try to
avoid $ sign.
7. White space is not permitted.
8. Special characters are not allowed.
9. A digit at the start is not allowed.
Rules in Using Variables
A variable cannot :
10.Subsequent characters maybe letters, digits, dollar signs, or
underscore characters.
11.Variable name must not be a keyword or reserved word.
Constant Variables
- refers to those variables whose value does not change during
program execution
Ex. final tax=26.7
Importants of Comments on Programs
1. Program documentation contains information that improves
the readability of the program.
2. Comments are part of program documentation because they
help others read and understand how the program works . The
compilers and interpreters however ignore comments and they
are not translated in machine language
Types of Comments in Java
1. Multiple line comments- are comments enclosed by /* and /*
Ex. /** test.java
**Author: Jake Cyrus
**/
2.Single line comments- are comments placed after // symbols.
Ex. // math.java
Identifiers
- are names given to various program elements such as variables,
functions and arrays
The ff. are the guidelines in creating
identifiers
1. It consist of letters and digits without space, in any other ,
except that the first character must be a letter.
2. Uppercase and lowercase are permitted but they are not
interchangeable. As an exception, an underscore may be used
as the first character of the identifier.
3. Reserve words and standard methods must not be used.
Examples of Valid Identifiers
Salary_Per_Month
Age
Grade23
_area_circle
PERSONS_NAME
Y
passing_grade75
Users_Name
Examples of Invalid Identifiers
Invalid Identifiers Reasons of Invalidity

3rd starts with a digit

BIR TAX contains space

%price contains a special character

while a reserved word


Reserved Words
- are keywords that have standard predefined meaning in Java.
These reserved words can be used only for intended purpose
and they cannot be used as programmer’s defined identifiers.
Example of Reserved Words
abstract char else Goto
assert class enum if
boolean const extends implements
break continue final import
byte default finally instance of
case do float int
catch double for interface
Data Type
-is a set of data that specifies the possible range of values of the
set, the operations that can be performed on the values, and the
way in which the values are stored in memory
Primitive Types
Type Name Wrapper class Value Range

byte java.lang.Byte integer -128 through +127

short java.lang.Short integer -32,768 through +32,767

int java.lang.Integer integer -2,147,483,648 through


+2,147’483’647

long java.lang.Long integer -9,223,372,036,854,775,


808 through
+9,223,372,036,854,775,
807
Primitive Types
Type Name Wrapper class Value Range

float java.lang.Float Floating point number ±1.401298E-45 through


±3.402823E+38
double java.lang.Double Floating point number ±4.94065645841246E-
324 through

boolean java.lang.Boolean Boolean true or false

char java.lang.Character UTF-16 code unit ‘\u0000’ through ‘\Uffff’


Java Operators
Java provides a rich set of operators environment. Java operators
can be divided into the following categories:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Conditional operators
Arithmetic Operators
OPERATOR DESCRIPTION

+ Adds two operands

- Subtract second operands from first

* Multiply two operand

/ Divide numerator by denominator


Arithmetic Operators
OPERATOR DESCRIPTION

% Remainder of division

++ Increment operator increases integer value by


one

-- Decrement operator decreases integer value by


one
Relational Operators
OPERATOR DESCRIPTION

== Check if two operand are equal

!= Check if two operand are not equal

> Check if operand on the left is greater than


operand on the right.

< Check operand on the left is smaller than right


operand
Relational Operators
OPERATOR DESCRIPTION

>= Check left operand is greater than or equal to


right operand

<= Check if operand on left is smaller than or


equal to right operand
Logical Operators
OPERATOR DESCRIPTION EXAMPLE

&& LOGICAL AND (a && b) is


false

|| LOGICAL OR (a | | b ) is true

! LOGICAL NOT ( ! a) is false


Bitwise Operators
OPERATOR DESCRIPTION

& bitwise AND


| bitwise OR
^ bitwise exclusive OR
<< left shift
>> right shift
Assignment Operators
OPERATO DESCRIPTION EXAMPLE
R

= assigns values from right side operands


to left side operand
a= b

+= adds right operand to the left operand and a+=b is same as a=a+b
assign the result to left

-= subtracts right operand from the left


operand and assign the result to left
a-=b is same as a=a-b

operand
Assignment Operators
OPERATO DESCRIPTION EXAMPLE
R

*= multiply left operand with the right


operand and assign the result to left
a*= b is same as a=a*b

operand

/= divides left operand with the right operand a/=b is same as a=a/b
and assign the result to left operand

%= calculate modulus using two operands and a%=b is same as a=a%b


assign the result to left operand
Escape Characters
• also called (escape sequences or escape codes ) in general are
used to signal an alternative interpretation of a series of
characters .
• In Java , a character preceded by a backslash (\) is an escape
sequence and has special meaning to the Java compiler.
Escape Characters
Unicode character \u3876 (\u followed by the hexadecimal Unicode
code point up to U+FFFF)
Octal escape \352 (octal number not exceeding 377, preceded by
backslash)
Line feed \n

Carriage return \r

Form feed \f
Escape Characters
backslash \\

Single quote \’

Double quote \”

tab \t

backspace \b

You might also like