SlideShare uma empresa Scribd logo
1 de 20
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)
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.
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.
//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.
//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
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.
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
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";
}
}
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
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
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
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
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.
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.
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
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.
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
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
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.
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

Mais conteúdo relacionado

Mais procurados

Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 

Mais procurados (19)

Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 
Core java
Core javaCore java
Core java
 
Java platform
Java platformJava platform
Java platform
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Core Java
Core JavaCore Java
Core Java
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Core java
Core javaCore java
Core java
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
Java
JavaJava
Java
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Java notes
Java notesJava notes
Java notes
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 

Destaque

Pemrograman Berorientasi Objek dengan Java
Pemrograman Berorientasi Objek dengan JavaPemrograman Berorientasi Objek dengan Java
Pemrograman Berorientasi Objek dengan Java
Ade Hendini
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
Haitham El-Ghareeb
 

Destaque (14)

X$Tables And Sga Scanner, DOAG2009
X$Tables And Sga Scanner, DOAG2009X$Tables And Sga Scanner, DOAG2009
X$Tables And Sga Scanner, DOAG2009
 
Pemrograman Berorientasi Objek dengan Java
Pemrograman Berorientasi Objek dengan JavaPemrograman Berorientasi Objek dengan Java
Pemrograman Berorientasi Objek dengan Java
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
Define location of Preplaced cells(http://www.vlsisystemdesign.com/PD-Flow.php)
Define location of Preplaced cells(http://www.vlsisystemdesign.com/PD-Flow.php)Define location of Preplaced cells(http://www.vlsisystemdesign.com/PD-Flow.php)
Define location of Preplaced cells(http://www.vlsisystemdesign.com/PD-Flow.php)
 
1 java - data type
1  java - data type1  java - data type
1 java - data type
 
Jnp
JnpJnp
Jnp
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Java I/O
Java I/OJava I/O
Java I/O
 
Esoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basicsEsoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basics
 
Object-Oriented Analysis and Design
Object-Oriented Analysis and DesignObject-Oriented Analysis and Design
Object-Oriented Analysis and Design
 
Structured Vs, Object Oriented Analysis and Design
Structured Vs, Object Oriented Analysis and DesignStructured Vs, Object Oriented Analysis and Design
Structured Vs, Object Oriented Analysis and Design
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
 

Semelhante a java: basics, user input, data type, constructor

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
amitbhachne
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
sotlsoc
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introduction
chnrketan
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
Muhammad Abdullah
 

Semelhante a java: basics, user input, data type, constructor (20)

Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
Java lab-manual
Java lab-manualJava lab-manual
Java lab-manual
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Introduction
IntroductionIntroduction
Introduction
 
java basic .pdf
java basic .pdfjava basic .pdf
java basic .pdf
 
Java introduction
Java introductionJava introduction
Java introduction
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introduction
 
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
 
SMI - Introduction to Java
SMI - Introduction to JavaSMI - Introduction to Java
SMI - Introduction to Java
 
Scala presentationjune112011
Scala presentationjune112011Scala presentationjune112011
Scala presentationjune112011
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 

Mais de Shivam Singhal (10)

English tenses
English tenses English tenses
English tenses
 
ofdm
ofdmofdm
ofdm
 
Chapter 1 introduction to Indian financial system
Chapter 1 introduction to Indian financial systemChapter 1 introduction to Indian financial system
Chapter 1 introduction to Indian financial system
 
Chapter 3 capital market
Chapter 3 capital marketChapter 3 capital market
Chapter 3 capital market
 
Chapter 2 money market
Chapter 2 money marketChapter 2 money market
Chapter 2 money market
 
project selection
project selectionproject selection
project selection
 
introduction to project management
introduction to project management introduction to project management
introduction to project management
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
java:characteristics, classpath, compliation
java:characteristics, classpath, compliationjava:characteristics, classpath, compliation
java:characteristics, classpath, compliation
 
10 8086 instruction set
10 8086 instruction set10 8086 instruction set
10 8086 instruction set
 

Último

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Último (20)

(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 

java: basics, user input, data type, constructor

  • 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. 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. 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. //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. //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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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