Documente Academic
Documente Profesional
Documente Cultură
Fundamental of Language
Machine Language:
Machine language is a lowest-level
programming language.
Machine
languages
are
only
language understood by computer
because they consist entirely of
numbers(Binary 0 and 1),Therefore
programmers use either a High-level
programming
language
or
an
assembly language.
Assembly Language:
An assembly language is a low-level
programming language,in which there
is a very strong correspondence
between the language and the
architectures
machine
code
instructions.
Program
written
in
high-level
language
are
translated
into
assembly language or machine
language by a compiler .
Every CPU has its own unique
machine language. Programs must
be rewritten or recompiled,therefore
Introduction of Java
Java is a programming language and
a platform.
Platform Any hardware or software environment
in which a program runs, known as a platform.
Since Java has its own Runtime Environment
(JRE) and API, it is called platform.
Java is a high-level programming language
originally developed by Sun Microsystems
and released in 1991. Java runs on a variety of
plateforms,such as Windows,Mac OS, and the
various version of Unix.
Java is an Object-Oriented language.It enables
us not only to organize our program code into
logical units called object but also to take
advantage of Encapsulation,inheritance, and
polymorphism.
Features of Java
There is given many features of java. They are also
known as java buzzwords.
Simple
Object-Oriented
Platform independent
Secured
Robust
Architecture neutral
Portable
Dynamic
Interpreted
High Performance
Multithreaded
Distributed
Simple
According to Sun, Java language is simple
because:
syntax is based on C++ (so easier for
programmers to learn it after C++).
removed many
confusing and/or rarely-used features e.g., explicit pointers,
operator overloading etc.
No need to remove unreferenced
objects because there is Automatic Garbage Collection in
java.Object-oriented
Object-oriented
Object-oriented means we organize our software as a
combination of different types of objects that incorporates
both data and behaviour.Object-oriented
programming(OOPs) is a methodology that simplify software
development and maintenance by providing some rules.Basic
concepts of OOPs are:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Objects are the basic run time entities is an object oriented system.
Class: A class is a collection of objects of similar type.
Encapsulation:
The wrapping up of data and functions into a single unit is knows as
encapsulation.
Data encapsulation is the most striking feature of a class(The data is not
accessible to the outside world and only those functions which are wrapper in
the class can access.)
Abstraction
Abstraction refers to the act of representing essential features without
including the background details or explanations.
D/f between Encapsulation or Abstraction
Take a example of laptop.
Inheritance:
Inheritance is one of the key feature of OOP language, In java a class can be
derived from another class. The derived class acquire all features of the base
class .
Provide the idea of reusability.
Java does not support multiple inheritance.
Polymorphism:
Polymorphism means the ability to take more than one form.
Benefits::
Data security is enforced.
Inheritance save time.
User defined data types can be easily constructed.
Large complexity in the software developed can be easily managed.
Platform Independent
Secured
Java is secured because:
No explicit pointer
Programs run inside virtual
machine sandbox.
Classloader- adds security
by separating the package for
the classes of the local file
system from those that are
imported
from
network
sources.
Bytecode Verifier- checks
the code fragments for illegal
code that can violate access
right to objects.
Security
Managerdetermines what resources a
class can access such as
reading and writing to the
local disk.
Robust
Robust simply means strong.
Java uses strong memory management.
There are lack of pointers that avoids security
problem.
There is automatic garbage collection in java.
There is exception handling and type checking
mechanism in java. All these points makes java
robust.
Architecture-neutral
There is no implementation dependent features
e.g. size of primitive types is set.
Portable
We may carry the java bytecode to any platform.
High-performance
Java is faster than traditional interpretation since
byte code is "close" to native code still
somewhat slower than a compiled language
Distributed
We can create distributed applications in
java. RMI and EJB are used for creating
distributed applications. We may access
files by calling the methods from any
machine on the internet.
Multi-threaded
A thread is like a separate program,
executing concurrently. We can write Java
programs that deal with many tasks at
once by defining multiple threads. The
main advantage of multi-threading is that
it shares the same memory. Threads are
important
for
multi-media,
Web
Hello Java-program
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
save this file as Simple.java
To compile:javac Simple.java
To execute:java Simple
Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
class keyword is used to declare a class in java.
public keyword is an access modifier which represents visibility, it means it is
visible to all.
static is a keyword, if we declare any method as static, it is known as static
method. The core advantage of static method is that there is no need to create
object to invoke the static method. The main method is executed by the JVM,
so it doesn't require to create object to invoke the main method. So it saves
memory.
void is the return type of the method, it means it doesn't return any value.
main represents startup of the program.
String[] args is used for command line argument.
System.out.println() is used print statement. The println method is a member
or method of the out object, which is a static data member of System class.
System is a final class ,so we cannot make its object.
temporary
Variable
Types of Variable
Local Variable
A variable that is declared inside the method is called local variable.
Instance Variable
A variable that is declared inside the class but outside the method is
called instance variable . It is not declared as static.
Static variable
A variable that is declared as static is called static variable. It cannot be
local.
class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable }
}//end of class
Data Types in Java
Operators in java
Scanner class
Command Line Argument is not user friendly, then we use Sanner class.
The java.util.Scanner class is a simple text scanner which can parse primitive types
and strings using regular expressions.Following are the important points about
Scanner:
A Scanner breaks its input into tokens using a delimiter pattern, which by default
matches whitespace.
A scanning operation may block waiting for input.
A Scanner is not safe for multithreaded use without external synchronization.
import java.util.*;
class sum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.println("Enter value of a and b=:");
a=sc.nextInt();
b=sc.nextInt();
a=a+b;
System.out.println("Sum of two nos:="+a);
}
}
Java Array
Java provides a data structure,
the array, which stores a fixed-size
sequential collection of elements of
the same type.
Declaring Array Variables:
dataType[] arrayRefVar;
Creating Arrays:
arrayRefVar = new dataType[arraySize];
//One-dimensional Array
Multidimensional array
Declaration:
int myArray[][]=new int[3][4];
foreach Loops:
JDK 1.5 introduced a new for loop, known as foreach loop or enhanced
for loop, which enables you to traverse the complete array
sequentially without using an index variable.
Example:
The following code displays all the elements in the array myList:
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}
This would produce following result:
1.9
2.9
3.4
3.5
String
Strings are a sequence of characters.
In the Java programming language,
strings are objects.
The Java platform provides the String
class to create and manipulate strings.
Creating Strings:
The most direct way to create a string is to
write:
String greeting = "Hello world!";
By new keyword
String s=new String("Welcome");//creates t
wo objects and one reference variable
class Simple{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the strin
g at the end
System.out.println(s);//will print Sachin because strings are
immutable objects
}
}
Output:Sachin
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
}
}
Output:true true false
//Example of equalsIgnoreCase(String) method
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="SACHIN";
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s3));//true
}
}
Output:false true
2) By == operator
The = = operator compares references not values.
//<b><i>Example of == operator</i></b>
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to sam
e instance)
System.out.println(s1==s3);//false(because s3 refers to insta
nce created in nonpool)
}
}
Output:true false
3) By compareTo() method:
compareTo() method compares values and returns an int which tells if the
values compare less than, equal, or greater than.Suppose s1 and s2 are
two string variables.If:
s1 == s2 :0
s1 > s2 :positive value
s1 < s2 :negative value
//<b><i>Example of compareTo() method:</i></b>
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}
Output:0 1 -1
2) By concat() method
concat() method concatenates the specified string to the end of
current string.
Syntax:
public String concat(String another){}
charAt() method
class Simple{
public static void main(String args[]){
String s="Sachin";
System.out.println(s.charAt(0));//S
System.out.println(s.charAt(3));//h
}
}
Output:S h
length() method
class Simple{
public static void main(String args[]){
String s="Sachin";
System.out.println(s.length());//6
}
}
Output:6
toUpperCase() and toLowerCase() method
class Simple{
public static void main(String args[]){
String s="Sachin";
System.out.println(s.toUpperCase());//SACHIN
System.out.println(s.toLowerCase());//sachin
System.out.println(s);//Sachin(no change in original)
}
}
Output:SACHIN sachin Sachin
StringBuffer class:
The StringBuffer class is used to created
mutable (modifiable) string. The StringBuffer
class is same as String except it is mutable i.e.
it can be changed.
class A{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is chan
ged
System.out.println(sb);//prints Hello Java
}
}
StringTokenizer in Java
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is khan"," ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
Output:my
Name
is
khan