Introduction TO JAVA
Rishi Ram Khanal
BIM(TU)
History of Java
Java was developed by James Gosling at Sun
Microsoftsystem and it was originated at Sun
Microsystem in 1991
JAVA is Everywhere
Java resides in mobiles,client machines,sever
machine,embedded device,smart phones,
cloud etc.
It shares the same basic features of the
language and libraries
Principle of java:write once,Run
anywhere(WORA)
What is Library?
Java Library is a collection of predefined
classes.
You can use these classes either by inheriting
them or by instantiating them.
JAVA flavours
Java se(core java)
Java EE (Advance java)
Java ME(Mobile Edition for java)
Version History Of JAVA
Version NO of classes NO of package
Java 1.0 212 8
Java 1.1 503 23
Java 2 1520 29
Java 5 3562 166
Java 6 3792 203
Java 7 4024 209
Features of JAVA
Simple
Object oriented Language
Distributed
Interpreted
Robust
Secure
Portable
Multi-threaded
Garbage Collector
Installation of Java
How to Compile?
Hello.java Hello.class
How to Run
Hello.class
OS
Interpreter(just-in-time compiler)
Installation and setup
• http://www.oracle.com/technetwork/java/javase
/downloads/jdk8-downloads-2133151.html
• After installation we will see a java folder where u
install the java.Inside java folder we will see two
folder
• Jdk: java development Kit used for to write java
program
• Jre:Java Runtime Enviroment used for to run the
java files
JDK and JRE
Java Development Kit contains tools needed
to develop the java programs
These tools could be
compiler(javac.exe),Application
Launcher(java.exe) etc
Jre: Java Runtime Environment
It contains JVM(java Virtual Machine) and java
Package classes(java Library)
JVM
JVM is platform dependent
The java Virtual Machine provides a platform
–independent way of executing code
Java Virtual Machine Interprets the bytecode
Into machine code depending upon the
underlying operating system and hardware
combination
First Program JAVA
Remember
• Java is a case sensitive language like c and c++
• Java is nearly 100% object oriented language
• In java, it is possible to make a function which
is not a member of any class(as we can do in c
and C++)
First Program
Public class HelloWorld
{
public static void main(String args[])
{
System.out.println(“HelloWOrld”);
}
}
FOR pratical
• Write a program in notepad and save in bin
folder of jdk Compile and run the program
Delete the file from bin folder and make another
program in notepad and save in Desktop and
compile and run the program and please note
the error u got and how u solve the error
DataTypes and Keywords In java
 Java Identifiers
All the java components require names.Names used for
classes, variables and method are called identifiers.In java ,
there are several points to remember about identifiers.
• All identifiers should begin with a letter (A to Z or a to z),
currency character ($) or an underscore (_).
• After the first character, identifiers can have any combination
of characters.
• A keyword cannot be used as an identifier.
• Most importantly identifiers are case sensitive.
• Examples of legal identifiers:age, $salary, _value, __1_value
• Examples of illegal identifiers: 123abc, -salary
Java Keywords
• These reserved words may not be used as constant or variable or any other identifier
names.
WhiteSpace
Literals, Comments,Separators
Literals
A literal is a source code representation of a fixed value. They are
represented directly in the code without any computation.
Comments
Java supports single-line and multi-line comments very similar to c and
c++. All characters available inside any comment are ignored by Java
compiler.
/* This is my first java program.
This will print 'Hello World' as the output
This is an example of multi-line comments. */
// This is an example of single line comment /* This is also an example of
single line comment. */
Data Types in JAVA
Details about Literals
Literals can be assigned to any primitive type variable. For example:
byte a =68;
char a ='A'
byte, int, long, and short can be expressed in decimal(
base 10),hexadecimal(base 16) or octal(base 8) number systems as well.
Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when
using these number systems for literals. For example:
int decimal=100;
int octal =0144;
int hexa =0x64;
String literals in Java are specified like they are in most other languages by
enclosing a sequence of characters between a pair of double quotes.
String and char types of literals can contain any Unicode characters. For
example:
char a ='u0001'; String a ="u0001";
Variable
• In Java, all variables must be declared before they
can be used. The basic form of a variable
declaration is shown here:
type identifier [= value][, identifier [= value]...];
int a, b, c;// declares three ints, a, b, and c.
int d =3, e, f =5;// declares three more ints,
initializing // d and f.
byte z =22;// initializes z.
double pi =3.14159;// declares an approximation of
pi.
char x ='x';// the variable x has the value 'x'.
• This chapter will explain various variable types
available in Java Language. There are three
kinds of variables in Java:
• Local variables
• Instance variables
• Class/static variables
TYPECASTING in JAVA
Impicit Type Casting
Explicit Type Casting
SCANNER OBJECT
ARRAYS
ARRAY
• Declaring Array Variables:
To use an array in a program, you must declare a variable to
reference the array, and you must specify the type of array the variable
can reference. Here is the syntax for declaring an array variable:
dataType[] arrayRefVar; // preferred way.
or
dataType arrayRefVar[]; // works but not preferred way.
Example:
• The following code snippets are examples of this syntax:
double[] myList; // preferred way.
or
• double myList[]; // works but not preferred way.
Creating ARRAY
• You can create an array by using the new operator with the following
syntax:
• arrayRefVar = new dataType[arraySize];
• The above statement does two things:
• It creates an array using new dataType[arraySize];
• It assigns the reference of the newly created array to the variable
arrayRefVar.
• Declaring an array variable, creating an array, and assigning the reference
of the array to the variable can be combined in one statement, as shown
below:
• dataType[] arrayRefVar = new dataType[arraySize];
• Alternatively you can create arrays as follows:
• dataType[] arrayRefVar = {value0, value1, ..., valuek};
• The array elements are accessed through the index. Array indices are 0-
based; that is, they start from 0 to arrayRefVar.length-1.
• Following statement declares an array variable, myList,
creates an array of 10 elements of double type and assigns its
reference to myList:
• double[] myList = new double[10];
• Following picture represents array myList. Here, myList holds
ten double values and the indices are from 0 to 9.
The 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.
• SCANNER OBJECT
PRACTICAL
• TypeCasting Problem
• Declare an array whose size is 5 .Process the
array and Print all the elements of array and
find total sum of all elements and Find
greatest number from that element
• Implement foreach Loop
• Create a class , take the two input from
keyboard and sum two number and process
the array by using Scanner

Introduction to java

  • 1.
    Introduction TO JAVA RishiRam Khanal BIM(TU)
  • 2.
    History of Java Javawas developed by James Gosling at Sun Microsoftsystem and it was originated at Sun Microsystem in 1991
  • 3.
    JAVA is Everywhere Javaresides in mobiles,client machines,sever machine,embedded device,smart phones, cloud etc. It shares the same basic features of the language and libraries Principle of java:write once,Run anywhere(WORA)
  • 4.
    What is Library? JavaLibrary is a collection of predefined classes. You can use these classes either by inheriting them or by instantiating them. JAVA flavours Java se(core java) Java EE (Advance java) Java ME(Mobile Edition for java)
  • 5.
    Version History OfJAVA Version NO of classes NO of package Java 1.0 212 8 Java 1.1 503 23 Java 2 1520 29 Java 5 3562 166 Java 6 3792 203 Java 7 4024 209
  • 6.
    Features of JAVA Simple Objectoriented Language Distributed Interpreted Robust Secure Portable Multi-threaded Garbage Collector
  • 7.
    Installation of Java Howto Compile? Hello.java Hello.class How to Run Hello.class OS Interpreter(just-in-time compiler)
  • 8.
    Installation and setup •http://www.oracle.com/technetwork/java/javase /downloads/jdk8-downloads-2133151.html • After installation we will see a java folder where u install the java.Inside java folder we will see two folder • Jdk: java development Kit used for to write java program • Jre:Java Runtime Enviroment used for to run the java files
  • 9.
    JDK and JRE JavaDevelopment Kit contains tools needed to develop the java programs These tools could be compiler(javac.exe),Application Launcher(java.exe) etc Jre: Java Runtime Environment It contains JVM(java Virtual Machine) and java Package classes(java Library)
  • 10.
    JVM JVM is platformdependent The java Virtual Machine provides a platform –independent way of executing code Java Virtual Machine Interprets the bytecode Into machine code depending upon the underlying operating system and hardware combination
  • 11.
    First Program JAVA Remember •Java is a case sensitive language like c and c++ • Java is nearly 100% object oriented language • In java, it is possible to make a function which is not a member of any class(as we can do in c and C++)
  • 12.
    First Program Public classHelloWorld { public static void main(String args[]) { System.out.println(“HelloWOrld”); } }
  • 13.
    FOR pratical • Writea program in notepad and save in bin folder of jdk Compile and run the program Delete the file from bin folder and make another program in notepad and save in Desktop and compile and run the program and please note the error u got and how u solve the error
  • 14.
    DataTypes and KeywordsIn java  Java Identifiers All the java components require names.Names used for classes, variables and method are called identifiers.In java , there are several points to remember about identifiers. • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). • After the first character, identifiers can have any combination of characters. • A keyword cannot be used as an identifier. • Most importantly identifiers are case sensitive. • Examples of legal identifiers:age, $salary, _value, __1_value • Examples of illegal identifiers: 123abc, -salary
  • 15.
    Java Keywords • Thesereserved words may not be used as constant or variable or any other identifier names.
  • 16.
  • 17.
    Literals, Comments,Separators Literals A literalis a source code representation of a fixed value. They are represented directly in the code without any computation. Comments Java supports single-line and multi-line comments very similar to c and c++. All characters available inside any comment are ignored by Java compiler. /* This is my first java program. This will print 'Hello World' as the output This is an example of multi-line comments. */ // This is an example of single line comment /* This is also an example of single line comment. */
  • 18.
  • 20.
    Details about Literals Literalscan be assigned to any primitive type variable. For example: byte a =68; char a ='A' byte, int, long, and short can be expressed in decimal( base 10),hexadecimal(base 16) or octal(base 8) number systems as well. Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals. For example: int decimal=100; int octal =0144; int hexa =0x64; String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes. String and char types of literals can contain any Unicode characters. For example: char a ='u0001'; String a ="u0001";
  • 21.
    Variable • In Java,all variables must be declared before they can be used. The basic form of a variable declaration is shown here: type identifier [= value][, identifier [= value]...]; int a, b, c;// declares three ints, a, b, and c. int d =3, e, f =5;// declares three more ints, initializing // d and f. byte z =22;// initializes z. double pi =3.14159;// declares an approximation of pi. char x ='x';// the variable x has the value 'x'.
  • 22.
    • This chapterwill explain various variable types available in Java Language. There are three kinds of variables in Java: • Local variables • Instance variables • Class/static variables
  • 23.
    TYPECASTING in JAVA ImpicitType Casting Explicit Type Casting SCANNER OBJECT ARRAYS
  • 24.
    ARRAY • Declaring ArrayVariables: To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable: dataType[] arrayRefVar; // preferred way. or dataType arrayRefVar[]; // works but not preferred way. Example: • The following code snippets are examples of this syntax: double[] myList; // preferred way. or • double myList[]; // works but not preferred way.
  • 25.
    Creating ARRAY • Youcan create an array by using the new operator with the following syntax: • arrayRefVar = new dataType[arraySize]; • The above statement does two things: • It creates an array using new dataType[arraySize]; • It assigns the reference of the newly created array to the variable arrayRefVar. • Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below: • dataType[] arrayRefVar = new dataType[arraySize]; • Alternatively you can create arrays as follows: • dataType[] arrayRefVar = {value0, value1, ..., valuek}; • The array elements are accessed through the index. Array indices are 0- based; that is, they start from 0 to arrayRefVar.length-1.
  • 26.
    • Following statementdeclares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList: • double[] myList = new double[10]; • Following picture represents array myList. Here, myList holds ten double values and the indices are from 0 to 9.
  • 27.
    The 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. • SCANNER OBJECT
  • 28.
    PRACTICAL • TypeCasting Problem •Declare an array whose size is 5 .Process the array and Print all the elements of array and find total sum of all elements and Find greatest number from that element • Implement foreach Loop • Create a class , take the two input from keyboard and sum two number and process the array by using Scanner