Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

java: basics, user input, data type, constructor

3,457 views

Published on

java presentation for basics, user input, data type, constructor

Published in: Engineering

java: basics, user input, data type, constructor

  1. 1. BASIC STRUCTURE – Fundamentals to Understand Class Attributes Primitive data types in Java Arrays – How to declare them Methods in Java Main method - It provides the control of program flow. Simple methods – We need object to access such methods Static methods - We DON’T need object to access such methods Constructors Object – An instance of a class How to instantiate? Calling of method/attribute. //Creating an object of Student class with name as “ram” Student ram = new Student(); //Accessing the method/attribute of ram ram.course=“M.C.A”; System.out.println(“New course of ram is: ”+ ram.course)
  2. 2. BASIC STRUCTURE – Fundamentals to Understand Single line comments: These comments start with // e.g.: // this is comment line Multi line comments: These comments start with /* and end with */ e.g.: /* this is comment line*/ Since Java is purely an Object Oriented Programming language – • We must have at least one class. • We must create an object of a class, to access SIMPLE methods/attributes of a class. Static methods are the methods, which can be called and executed without creating objects. e.g. main() method.
  3. 3. BASIC STRUCTURE – Flow of Execution JVM contains the Java interpreter, which in turn calls the main () method using its Classname.main () at the time of running the program. main() method must contain String array as argument. Since, main () method should be available to the JVM, it should be declared as public. Write the main method. Execution starts from main() method. [Plz note, we do not need an object to access/execute main() method becuase it is static.] Write the first class containing the main() method. Name of the class and the name of the file should be EXACLTY same. Import the required packages. By default import java.lang.* A package is a kind of directory that contains a group of related classes and interfaces. A class or interface contains methods.
  4. 4. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } PROGRAM EXECUTION – STEP I Enter main method. The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method.
  5. 5. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } PROGRAM EXECUTION – STEP II Execute Statement 1
  6. 6. ESCAPE SEQUENCE Java supports all escape sequence which is supported by C/ C++. t Insert a tab in the text at this point. b Insert a backspace in the text at this point. n Insert a newline in the text at this point. r Insert a carriage return in the text at this point. f Insert a form feed in the text at this point. ' Insert a single quote character in the text at this point. " Insert a double quote character in the text at this point. Insert a backslash character in the text at this point.
  7. 7. USER INPUT There are three ways to take input from a user: 1. User input as Command Line Argument 2. User input using Scanner class object 3. User input using BufferedReader class object
  8. 8. USER INPUT - Command Line Argument - Example class Student{ String course; String address; Integer semester; } import java.lang.*; class Test{ public static void main(String[] args){ Student ramesh = new Student(); //Set the values to attributes ramesh.course="B.Tech"; ramesh.address=“Delhi"; } }
  9. 9. USER INPUT - Command Line Argument - Example class Student{ String course; String address; Integer semester; } import java.lang.*; class Test{ public static void main(String[] args){ Student ramesh = new Student(); //Set the values to attributes ramesh.course=args[0]; ramesh.address=args[1]; } } javac Test.java java Test B.Tech Delhi
  10. 10. USER INPUT - Command Line Argument - Example class Student{ String course; String address; Integer semester; } import java.lang.*; class Test{ public static void main(String[] args){ Student ramesh = new Student(); //Set the values to attributes ramesh.course=args[0]; ramesh.address=args[1]; int i = Integer.parseInt(args[2]); ramesh.semester=i } } javac Test.java java Test B.Tech Delhi 5
  11. 11. USER INPUT - using Scanner class object import java.util.Scanner; class Test{ public static void main(String[] args){ Student ramesh = new Student(); Scanner ob=new Scanner(System.in); System.out.println("Please enter the course"); String courseTemp=ob.nextLine(); System.out.println("Please enter sem no. "); int semesterTemp=ob.nextInt(); //Set the values to attributes ramesh.course=courseTemp; ramesh.semester=semesterTemp; } } For more: http://www.tutorialspoint.com/java/util/java_util_scanner.htm
  12. 12. USER INPUT - using BufferedReader class object import java.io.BufferedReader; import java.io.Exception; import java.io.InputStreamReader; public class Test{ public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter String"); String s = br.readLine(); System.out.print("Enter Integer:"); try{ int i = Integer.parseInt(br.readLine()); }catch(Exception e){ System.err.println("Invalid Format!"); System.out.println(e); } } } For more: http://stackoverflow.com/questions/2231369/scanner-vs-bufferedreader
  13. 13. BASIC STRUCTURE – Fundamentals to Understand Class Attributes Primitive data types in Java Arrays – How to declare them Methods in Java Main method - It provides the control of program flow. Simple methods – We need object to access such methods Static methods - We DON’T need object to access such methods Constructors Object – An instance of a class How to instantiate? Calling of method/attribute.
  14. 14. CONSTRUCTOR A class contains constructors that are invoked to create objects. There are basically two rules defined for the constructor: • Constructor name must be same as its class name • Constructor must have no explicit return type Class Bicycle{ int gear, cadence, speed; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } } To create a new Bicycle object called myBike, a constructor is called by the new operator: Bicycle myBike = new Bicycle(30, 0, 8); new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.
  15. 15. CONSTRUCTOR Although Bicycle only has one constructor, it could have others, including a no-argument constructor: //Inside Bicycle class public Bicycle() { gear = 1; cadence = 10; speed = 0; } //Outside Bicycyle class – e.g. Inside main method Bicycle yourBike = new Bicycle(); above statement invokes the no-argument constructor to create a new Bicycle object called yourBike. • Both (or more) constructors could have been declared in same class - Bicycle. • Constructors are differentiated by Java Interpreter based on the SIGNATURE. • SIGNATURE = No. of arguments + Data type of arguments + Order of arguments
  16. 16. CONSTRUCTOR class Student{ String course; String address; int semester; //Default Constructor – NO ARGUMENT public Student(){ //Creates space in memory for the object and initializes its fields to defualt. } //Parameterized Constructor – ALL ARGUMENTS public Student(String s1, String s2, int i1){ this.course=s1; this.course=s2; this.semester=i1; } //Parameterized Constructor – FEW ARGUMENTS public Student(String s1){ this.course=s1; } } Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
  17. 17. CONSTRUCTOR import java.lang.*; class Test{ public static void main(String[] args){ //Get the values in to TEMPORARY variables String temp1 = args[0]; String temp2 = args[1]; int temp3 = args[2]; Student ramesh = new Student(temp1,temp2,temp3); System.out.println(ramesh.course); System.out.println(ramesh.address); System.out.println(ramesh.semester); } } javac Test.java java Test B.Tech Delhi 5
  18. 18. PRIMITIVE DATA TYPE in JAVA Type Contains Default Size Range byte Signed integer 0 8 bits -128 to 127 short Signed integer 0 16 bits -32768 to 32767 int Signed integer 0 32 bits -2147483648 to 2147483647 float IEEE 754 floating point 0.0f 32 bits ±1.4E-45 to ±3.4028235E+38 long Signed integer 0L 64 bits -9223372036854775808 to 9223372036854775807 double IEEE 754 floating point 0.0d 64 bits ±4.9E-324 to ±1.7976931348623157E+308 boolean true or false FALSE 1 bit NA char Unicode character 'u0000' 16 bits u0000 to uFFFF
  19. 19. FEATURES of JAVA • Simple: Learning and practicing java is easy because of resemblance with C and C++. • Object Oriented Programming Language: Unlike C++, Java is purely OOP. • Distributed: Java is designed for use on network; it has an extensive library which works in agreement with TCP/IP. • Secure: Java is designed for use on Internet. Java enables the construction of virus-free, tamper free systems. • Robust (Strong enough to withstand intellectual challenge): Java programs will not crash because of its exception handling and its memory management features. • Interpreted: Java programs are compiled to generate the byte code. This byte code can be downloaded and interpreted by the interpreter. .class file will have byte code instructions and JVM which contains an interpreter will execute the byte code.
  20. 20. FEATURES of JAVA • Portable: Java does not have implementation dependent aspects and it yields or gives same result on any machine. • Architectural Neutral Language: Java byte code is not machine dependent, it can run on any machine with any processor and with any OS. • High Performance: Along with interpreter there will be JIT (Just In Time) compiler which enhances the speed of execution. • Multithreaded: Executing different parts of program simultaneously is called multithreading. This is an essential feature to design server side programs. • Dynamic: We can develop programs in Java which dynamically change on Internet (e.g.: Applets). P l e a s e G o o g l e / Re a d m o re fo r R E D h i g h l i g h t e d c o l o r ke y w o rd s F O R M O R E I N F O / O F F I C I A L L I N K - JAVA h t t p : / / d o c s . o ra c l e . c o m / j a v a s e / t u t o r i a l / j a v a / j a v a O O / i n d e x . h t m l

×