Professional Documents
Culture Documents
Unit 1 Part - 1 Java Basic
Unit 1 Part - 1 Java Basic
Java is easy.
Java Advantages
Portable - Write Once, Run Anywhere
◦ The use of the same bytecode for all JVMs on all platforms
allows Java to be described as a write once, run anywhere
programming language
JAVA COMPILER
(translator)
JAVA INTERPRETER
(one for each different system)
Package Statement
Import Statements
Interface Statements
Class Declarations
main Method Class
{
}
HelloWorld (standalone)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
15
System.out.println(“Hello World”);
System is really the java.lang.System class.
This class has a public static field called out
which is an instance of the java.io.PrintStream
class.
So when we write System.out.println(), we are
really invoking the println() method of the “out”
field of the java.lang.System class.
16
Java program layout
byte, short, int, long data types are singed, integral value in 2’s
complement.
float is signed, single-precision floating point value and
double is signed, double-precision floating point value.
whereas char is unsigned integral value.
Data types and its default value
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
boolean False
== Equal (careful)
!= Not equal
>= Greater than or equal
<= Less than or equal
> Greater than
< Less than
Statements & Blocks
while(response == 1) {
System.out.print( “ID =” + userID[n]);
n++;
response = readInt( “Enter “);
}
do {
System.out.print( “ID =” + userID[n] );
n++;
response = readInt( “Enter ” );
}while (response == 1);
Java Source
javadoc HTML Files
Code
javac
java jdb
Outout
Comments are almost like C++
/* This kind of comment can span multiple lines */
/**
* This kind of comment is a special
* ‘javadoc’ style comment
*/
What is a class?
Early languages had only arrays
◦ all elements had to be of the same type
Encapsulation
◦ Objects hide their functions
(methods) and data
(instance variables)
Inheritance
◦ Each subclass inherits all car Super class
variables of its superclass
Polymorphism auto-
manual
matic Subclasses
◦ Interface same despite
different data types
draw() draw()
Naming conventions
Java is case-sensitive;
maxval, maxVal, and MaxVal are three different names
Syntax:
modifiers type name ( parameter list ) [throws exceptions ]
Eg.
1) public float convertCelsius (float tCelsius ) {}
Rectange r,
Date d1, d2;
Here r , d1 are references(variables) of type Rectangle ,
Date respectively.
class Person {
String name;
int age;
void birthday ( ) {
age++;
System.out.println (name + ' is now ' + age);
}
}
Creating and using an object
Person john;
john = new Person ( );
john.name = "John Smith";
john.age = 37;
Person mary = new Person ( );
mary.name = "Mary Brown";
mary.age = 33;
mary.birthday ( );
e.g. 2)
Simple Class and Method
Class Fruit {
int grams;
int cals_per_gram;
int total_calories() {
return(grams*cals_per_gram);
}
}
Public/private
• java.lang
• Classes for various data types, running processes, strings, threads, exceptions and much more.
• They are automatically imported.
• java.util
• Utility classes for dates, vectors, random numbers etc.
• java.io
• Classes for various types of input and output.
• java.awt
• Abstract Window Toolkit (AWT) classes for the GUI interface. Such as windows, dialog
boxes, buttons and text fields and more.
• java.net
• Classes for networking, URLs/clients/server sockets.
• java.applet
• Classes for creating and implementing applets.
• java.awt.image
• Classes for managing and manipulating images.
• java.sql
• This package encompasses java database connectivity (JDBC) enables to access RDBMS.
• java.security
• Java security packages to use encryption in java program for secure data transfer among
clients and servers.