Java Basic day-1


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Basic day-1

  1. 1. Nibble MATRIX Summer Training First day.Environment SettingSelect Start -> Computer -> System Properties -> Advanced system settings ->Advanced-> environment variable-> system variable ->pathVariable name= PathVariable value= ; C:Program FilesJavajdk1.6.0_02bin;.Ok and OKSelect
  2. 2. Java:java is an object oriented, internet programming languge .Its main feature is1-plateform independent on machine2- securitythere are two tools which is for compliling and running java programsjavac - it is compiler which converst java source code to byte code ie.class file. This bytecode is standard for all platforms, machines or operating - this is interpreter. this interprets the .class file based on a particular platform andexcutes them.e jvm -> java virtual machine comes into play. jvm for windows will bedifferent from jvm for solarais or linux . but all the jvm take the same byte code andexecutes them in that platform.Source code -> javac ->Universal byte codeUniversal byte ->jvm/java -> execute them on a particular machine.Syntex Of Defining Class:Class Identifier{Member definition}
  3. 3. /// First Pgm:-public class Add {void Addition(int a ,int b){int c=a+b;System.out.println("Sum of 2 number="+c);}public static void main(String[] args) {Add obj=new Add();obj.Addition(10, 20);}}Steps to//Run java pgm// javac Add.javaJava AddNaming Convension in Java:Class: 1st letter of each world of class is capitalexample: Welcome , ArrayIndexOutOfBoundExceptionMethod: ist letter of each word except first world is capitalExample:- nextInt() , nextLine()
  4. 4. Keywords:-Keywords are reserved identifiers that are predefined in the language and cannotbe used to denote other entities. All the keywords are in lowercase, and incorrectusage results in compilation errors.Identifiers :-In Java an identifier is composed of a sequence of characters, where each character can beeither a letter, a digit, a connecting punctuation (such as underscore _), or any currency symbol(such as $, ¢, ¥, or £). Identifiers can be used to denote classes, methods,variables, and labelsExamples of Legal Identifiers:number, Number, sum_$, bingo, $$_100,Examples of Illegal Identifiers:48chevy, all@hands, grand-sum
  5. 5. Lifetime of Variables:- is, the time a variable is accessible during execution, itsdetermined by the context in which it is declaredThere are context of variable-Instance variables :- members of a class and created for each object of the classStatic variables :- also members of a class, but not created for any object of the class and,therefore, belong only to the classLocal variables :- declared in methods and in blocks and created for each execution of themethod or blockDefault Values :-Data Type Default Value Default data typeboolean falsechar u0000Integer (byte, short, int, long) 0L for long, 0 for others Intand can be specified by long byappending the suffix (L or l)Floating-point (float, double) 0.0F or 0.0D double but it can be explicitlydesignated by appending the suffixD (or d) to the valueReference types null
  6. 6. type casting - to convert one cast to another castimplicit type casting : auto - lower to higherExample: byte b = 100;int a = b; - validexplicit type casting: user - higher to lower/ String to a = 200;byte b = a; - error , need to explicit type castingbyte b = (byte)a;Exmple:public class Test {void disp(){int a = 300;byte b = (byte)a;System.out.println("value of b="+b);}public static void main(String[] args) {Test obj=new Test();obj.disp();}}Ans= value of b=44data types in java -Data Bytebyte 1short 2int 4long 8float 4double 8char 2boolean true / false
  7. 7. String to inti- int a = Integer.parseInt(aa[0]);ii- int a = Integer.valueOf(aa[0]).intValue();String to floati- float a = Float.parseFloat(aa[0]);ii- float a = Float.valueOf(aa[0]).floatValue();String to doublei- double a = Double.parseDouble(aa[0]);ii- double a = Double.valueOf(aa[0]).doubleValue();String to longi- long a = Long.parseLong(aa[0]);ii- long a = Long.valueOf(aa[0]).longValue();Object creation in C++int a; - var declareClassname objname ;ex -C1 o; - default or no argument passing Constructor javaClassname objname = new ClassConstructor();ex -C1 o = new C1(); - default or no argument passing Constructorcalling.working with objects in javaObject var - before memory allocationClassName objname;ex -C1 o; - o is a kind of object var
  8. 8. Instance var - after memory allocationobjname = new ClassConstructor();ex -o = new C1(); - now o is is a kind of instance var.a = 10; - now a is a initialize varint a = 100; - declare + initializeC1 o = new C1(); - object + instanceRef object var - when one object hold the ref of another objectC1 o = new C1();C1 n = o; - n hold the ref of o - operator , it is for -* call the Constructor* allocate memory for Object* return ref of class for Object.C1 o = new C1();o.disp(); - C1 class disp.