Download introduction to java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
INTRODUCTION TO JAVA
I am including each and every bit which was covered or spoken when
I was in Institute & few more things from my own. Some of you might
be knowing basic things already. Just have a glimpse. But remind you
from my experience and point of view I am telling, Jspiders notes are
best for Technical face 2 face. For technical written
(Programmimg/Objective), have to put much more effort. Nothing can
be spoon feeded guys. Guys I cant make charts & diagrams, it takes
lot of time so I am trying to explain it in my words. I am also including
error codes in this file and whats the reason & solution for that error
which a programmer should know.Please practice more than learning
friends. If any grammatical mistake, please forgive me for that (LOL).
First program is written and then the concepts related to that
program. For any query even in C/C++, UNIX, you can always revert
me back.
IN THIS FILE MEANS POINTS TO REMEMBER
 MEANS A QS OR PROGRAM.
Processing of java program:Java Programmer (usually java file) - Then-Java ComplierThen - Byte Code (.class file)
- Then-JVMThen-in machine language(which is your output).
Translator s in Java:1-Compiler
2-Interpreter (JVM)
 Java has both type of translator,i.e, Java compliler & JVM.
 Java compiler translates .java file into .class file which is in the
form of bytecode.Then,JVM translates .class file into machine
language code line by line & generate output.
 That’s why java is slow.
 Is Java Platform independent ?
 Yes,Java is platform independent, java runs on every OS,we
only have to run the .class file instead of writing the whole code.
 But Bytecode is platform dependent.
 Java has different environment for different OS.
Java has 3 Editions:1-JSE (Java Standard Edition) for core java.
2-JEE (Java Enterprise Edition ) for web Applications.
3-JME (Java Micro Edition ) for Mobile Apps.
 Instruction is made up of keywords, identifiers & literals.
 Keywords - These are predefined words.All are in lowercase.Have
their own meaning.
 Identifiers –Names given by programmer.Keywords should not be
used as identifier.Alphanumeric character.Name should start with an
alphabet.
 Literals – Values used in the program.Numeric literals,String literals,
Boolean literals.
Class Name + Clasee declaration = Java class.
 Filename should have same name as the classname, not a rule but a
convention which is followed.
NOTE- You will find program starts & program ends statements in
System.out.println in almost all programs in this file in order to
understand the flow of program well.
Error 1



Class Sample1
{
//empty class
}
Error name-Main Method not found in class Sample1-Thrown by JVMWhenever a java class is executed without main method then JVM
throws this error.To run any java class, class should contain a
mandatory main method.
Correct code






Class Sample1
{
public static main method (String args [])
{
//Empty code
}
}
Developing a Java program includes 3 stages:1-Coding-Writing java program is known as coding,the java program should
be written with java class.
 Syntax to write java class is:Class ClassName
 The ClassName is an identifier for the class.
 If the name is consisting of more than two words, the starting ;etter of
each word should be in uppercase.
 Java program must be saved in a file with the extension .java
2-Compilation-Compilation is a process of translating the java statements
to the bytecode, this translation is done by the java
compiler.
 The translated bytecode is stored in a file with an extension .class
 Syntax for compilation :Javac filename.java
 Whenever we have to compile a java file in a command prompt,the
working folder should be the folder where the java file is stored.
 After compilation, the class files are stored in the location where the
java file is saved, this is done by compiler by default.
3-Execution- Whenever we have to use the result of the program, the
program should be executed, running a java program is
known as execution.
 To run any java class, we need JRE on the system.The JVM present
inside JRE, executes each statement of the class file sequentially.
 Syntax to execute a java class is Java ClassName
NOTE-JVM starts the execution of java program only if it contains
main method in the java class, otherwise it throws an error.
NOTE- System.out.println(“Hello Guys”);
 This statement is used to print message on the screen.
Where :
 System is s built in class in java.
 out is an object of output screen.
 Println is an instance member of the object.











Class Sample2
{
public static void main(String args [])
{
//println method is overloaded
System.out.println(1234); // Numeric literal
System.out.println(12.34);
System.out.println(‘ A ’); //Character literal
System.out.println(true); //Boolean literal
}
}
 The println() method is an overloaded method which can take string/
numeric/ character/ Boolean literals as argument.
NOTE- System.out.println(null);
 It cannot take null as an argument.









Class Sample3
{
Public static void main(String args [])
System.out.println(“Program starts”);
System.out.println(“Hello + Welcome to java”);
System.out.println(12+12);
System.out.println(“Addition of two numbers is :”+12+12);
System.out.println(“Program ends”);
}
O/P-Program starts
Hello Welcome to java
24
Addition of two numbers is :1212
Program ends
 In Java, the ‘ + ‘ operator is used as concatenation operator & also as
an addition operator.
 If ‘ + ‘ is used between two string literals, then it act as a
concatenation operator.
 If ‘ + ‘ is used between numeric literals, then it act as an addition
operator.
 If ‘ + ‘ is used in expression with string & numeric literal, then it will be
acting as concatenation operator.
VARIABLES :-












class Sample5
{
public static void main(String[] args)
{
System.out.println("Program Starts");
int STID;//declaration
STID=1234; //definition
System.out.println("Student ID is: "+STID);
System.out.println("Program ends");
}
}
O/P:-Program starts
Student ID is 1234
Program ends
 Variables are used to store information in a program.
 In java, we can create two types of variables
--Primitive variables & Reference variables :- Primitive variables are
used to store primitive data types whereas reference variables are used
to store address.
If we have to use variable, we have to follow three steps :-
1-Variable declaration -
 In this step, a memory is reserved to store
the data by declaring the variable.
 Syntax for primitive variable declaration Datatype variableName
 Java support 8 datatypes to store different types of value.
-int
-byte
-float
-double
-short
-long
-char
-boolean
 There is no datatype for strings,Instead we have String
classes(will be discussed later).
2-Variable Initialization : Storing a value inside the variable is known as variable
initialization or declaration.
 When initializing the variable, the value should be same type as
the variable declared.
 Every variable must be initialized before using it.
3-Variable usage : Utilizing the variable in any operation is known as variable
usage.
 If we don’t initialize & use the variable, compiler throws error.
 A variable can be declared & initialized at the same time.
 class Sample7
{

public static void main(String[] args)

{

System.out.println("Program starts");

byte b=1;

int i=2;

long l=3l; //always use ' l ' with long else it will be
//treated as int

short s=4;

double d=5.0;

float f=6.0f; //always use ' f ' with float else throws an
//error

char c='a';

boolean n=true;

System.out.println("value of datatypes are "+b+"
"+i+" "+l+s+d+f+c+n);

System.out.println("Program ends");

}
}
//Program to show default values of global variables.In java they don’t have
garbage values like C rather,default values.
class Sample8
{
// global (class level variables)
static int i;
static double d;
static boolean b;
static char c;
public static void main(String[] args)
{
int j=2; // local instance variable
System.out.println("Program starts.....");
System.out.println("value of i is "+i);
System.out.println("value of d is "+d);
System.out.println("value of b is "+b);
System.out.println("value of c is "+c);
System.out.println("value of j is "+j);
System.out.println("....Program ends");
}
}
O/P:- Program starts….
value of i 0
value of d 0.0
Value of b false
value of c
value of j 2
Program ends….
 Variable declared in a body of a method or a context is known as
local variable.
 Scope of the local variable is limited tto the context.
 A variable declared within class body is known as global variable.
 If the global variables are not initialized by the programmer, st the
time of compilation, compiler initializes based on the date types.
It initializes :




‘0’ for byte, short, long, int
‘0.0’ for double, float.
False for Boolean.
Prints nothing for char.