Unit-1-Intro To Java
Unit-1-Intro To Java
Java Fundamentals
Compiled &
Interpreted
Java is easy to learn and its syntax is quite simple, clean and
easy to understand.
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.
2.Object Oriented:
Java is object oriented language like c++,but it does not
support multiple inheritance.
Java can be easily extended as it is based on Object Model.
3.Security Manager:
It implements security policy of JVM. It determines what
resources a class can access such as reading and writing
to the local disk.
7)Architectural Neutral:
Compiler generates bytecodes, which can be executed by any
processor,nothing to do with particular computer
architecture.
hence a Java program is easy to intrepret on any machine.
10)High Performance:
12)Dynamic:
It supports Dynamic memory allocation due to this
memory wastage is reduce and improve performance
of the application.
Class Hello
{
public static void main(String args[])
{
System.out.println("Hello Java World");
}
}
1
Output: Hello Java World
Asst. Prof. Rameshwari H.
public keyword is an access modifier which represents
visibility, it means it is visible to all,hence it is accessible to
all other classes.
Java Tools:
Syntax:
java classname
E.g.
java Hello
Syntax:
Javap classname
Options of javap:
-help
-l
-c
-s
-sysinfo
1 Asst. Prof. Rameshwari H.
-constants
-version
javadoc
Syntax:
Javadoc source files
Syntax :
Jdb classname
Class : Name of class to begin debugging.
E. g. jdb proj
Syntax:
Save your prog containing applet as prog.java
first compile your prog using command javac
prog.java
execute it using command – appletviewer
prog.class
Documentation Section
Package Statement
Import Statement
Interface Statements
Class Definitions
Main Method class
{
Main Method Definition
}
Reserved Words
Identifiers
Literals
Operators
Separators
Types of Variable
There are three types of variables in java
1.local variable
2.instance variable
3.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.
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
Arithmetic Operators
+ Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
&& Logical-AND
|| Logical-OR
! Logical NOT
Conditional Operators
Java.io package
class inputdemo1
{
public static void main(String args[]) throws IOException
{
}
Command Line Arguments
The arguments passed from the console can be received in the java
program and it can be used as an input.
You can pass N (1,2,3 and so on) numbers of arguments from the
command prompt.
Syntax:
Data_type variable1=(Data_type)variable2;
Eg:
int x=10;
byte y=(byte)x; //Casting into smaller type results into loss of data.
•If statement
•Switch statement
•For statement
if(condition)
{
Block of statements
}
else
{
Block of statements
}
While(condition)
{
____________________
____________________
____________________
}
do
{
______________________
______________________
}while(cond);
1
} Asst. Prof. Rameshwari H.
}
For loop
Block Statements;
}
Break Statement
The break statement breaks out of a loop. It jumps out of
the current iteration and ends the immediate loop.
Syntax:
break;
if(i==6)
break;
}
}
}
Syntax:
Continue;
if(i%2==0)
continue;
System.out.println(i);
}
}
}
example:
char[] ch={‘w’,’e’,’l’,’c’,’o’,’m’,’e’'};
For Example:
String s="welcome";
char ch[]={'H','e','l','l','o'};
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}1 Asst. Prof. Rameshwari H.
StringBuffer
StringBuffer is a peer class of String that provides much of
the functionality of strings.
The major difference between the String and StringBuffer is
that String class creates string of fixed length, StringBuffer
creates strings of flexible length that can be modified in terms of
both length and content.
StringBuffer Constructors
StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)
1 Asst. Prof. Rameshwari H.
String StringBuffer
1.StringBuffer class is
1.String class is immutable.
mutable.
String Length():
length():This method returns the number of characters
contained in the string object.
Example:
String p = “Welcome";
int len = p.length();
It returns length 7.
String p = “Welcome";
char c = p.charAt(3);
Output: c
Syntax:
Output:demo
Syntax:
Syntax:
String s1 = "one";
String s2 = s1.concat("two");
puts the string “onetwo” into s2. I
Output: Hewwo
1 Asst. Prof. Rameshwari H.
substring( )
You can extract a substring using substring( ). It has two
forms.
Syntax of first form:
String substring(int startIndex)
Here, startIndex specifies the index at which the substring
will begin
insert(): Inserts the string representation of the argument into the StringBuffer at the specified position
For example= StringBuffer sb = new StringBuffer("Hello");
sb.insert(5, " World"); // sb now contains "Hello World“
Syntax:
int length( )
int capacity( )
Output:
buffer = Hello
length = 5
capacity = 21
1 Asst. Prof. Rameshwari H.
insert( ):
Constructors:
class insertDemo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
Output:
1 Asst. Prof. Rameshwari H.
I like Java!
reverse( )
You can reverse the characters within a StringBuffer object
using reverse( ).
Syntax:
StringBuffer reverse( )
Output:
abcdef
fedcba