Java Tutorial

Java is a general purpose, high-level programming language developed by Sun Microsystems. 

Java is an object-oriented language similar to C++, but simplified to eliminate language features that cause common programming errors.

Where it is used?

According to Sun, 3 billion devices run java. There are many devices where java is currently used. Some of them are as follows:

1. Desktop Applications such as acrobat reader, media player, antivirus etc.
2. Web Applications such as irctc.co.in, javatpoint.com etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games etc.


Types of Java Applications

There are mainly 4 type of applications that can be created using java programming:

1) Standalone Application

It is also known as desktop application or window-based application. An application that we need to install on every machine such as media player, antivirus etc. AWT and Swing are used in java for creating standalone applications
.
2) Web Application

An application that runs on the server side and creates dynamic page, is called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in java.

3) Enterprise Application

An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications.

4) Mobile Application

An application that is created for mobile devices. Currently Android and Java ME are used for creating mobile applications.

Stream
A stream is a sequence of data.In Java a stream is composed of bytes. It's called a stream because it's like a stream of water that continues to flow.

In java, 3 streams are created for us automatically. All these streams are attached with console.

1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream

Let's see the code to print output and error message to the console.

System.out.println("simple message");  
System.err.println("error message");  
Let's see the code to get input from console.
int i=System.in.read();//returns ASCII code of 1st character  
System.out.println((char)i);//will print the character  


Data Types in Java
In java, there are two types of data types

  • primitive data types
  • non-primitive data types


Data Type
Default Value
Default size
boolean
false
1 bit
char
'\u0000'
2 byte
byte
0
1 byte
short
0
2 byte
int
0
4 byte
long
0L
8 byte
float
0.0f
4 byte
double
0.0d
8 byte


Types of Variable
There are three types of variables in java

  • local variable
  • instance variable
  • static 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  



Operators in java

Operator in java is a symbol that is used to perform operations. There are many types of operators in java such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator.

Operators
Precedence
postfix
expr++ expr--
unary
++expr --expr +expr -expr ~ !
multiplicative
* / %
additive
+ -
shift
<< >> >>>
relational
< > <= >= instanceof
equality
== !=
bitwise AND
&
bitwise exclusive OR
^
bitwise inclusive OR
|
logical AND
&&
logical OR
||
ternary
? :
assignment
= += -= *= /= %= &= ^= |= <<= >>= >>>=


Loop

Java programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.

Loop Type
Description
While loop
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
For loop
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
Do..while loop
Like a while statement, except that it tests the condition at the end of the loop body

While loop


For loop


Do…while loop


Decision making statement

Java programming language provides following types of decision making statements. Click the following links to check their detail.

Statement
Description
If statement
An if statement consists of a boolean expression followed by one or more statements.
If..else statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Nested if statement
You can use one if or else if statement inside another if or else if statement(s).
Switch statement
switch statement allows a variable to be tested for equality against a list of values.

If statement


If…else statement


If...elseif statement


Switch statement



Java - Arrays

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;   // preferred way.
or

dataType arrayRefVar[];  //  works but not preferred way.


Java String class methods

The java.lang.String class provides many useful methods to perform operations on sequence of char values.


No.
Method
Description
1
returns char value for the particular index
2
returns string length
3
returns substring for given begin index
4
returns a joined string
5
checks the equality of string with object
6
checks if string is empty
7
concatinates specified string
8
returns trimmed string omitting leading and trailing spaces
9
returns splitted string matching regex
10
returns splitted string matching regex and limit
11
returns specified char value index
12
returns specified char value index starting with given index
13
returns string in lowercase.
14
returns string in uppercase.



Menu Parser Program



Java Tutorial Java Tutorial Reviewed by Unknown on 03:05:00 Rating: 5

No comments: