SlideShare uma empresa Scribd logo
1 de 22
Introduction to Java


               OOSSE Programming with Java
                       Lecture 1




Dec 21, 2012   OOSSE - Java Lecture 1        1
Objectives
 In this lecture, we will:
 • Introduce Java and the Java Virtual Machine
 • Discuss the basic object oriented concepts
 • Define classes and instances
 • Review the structure and syntax of a Java program
 • Introduce the Scanner class and simple I/O




Dec 21, 2012   OOSSE - Java Lecture 1        2
Introduction to Java
 • Java is a high level object oriented programming language
 • Java was designed to be:
     –   Simple
     –   Object oriented
     –   Distributed
     –   Robust
     –   Secure
     –   Architecture-Neutral
     –   Portable
     –   Multithreaded




Dec 21, 2012       OOSSE - Java Lecture 1     3
A Simple Java Application
 // A simple Hello World Java Application

 public class Hello
 {
    public static void main (String[] args)
    {
         System.out.println ("Hello World");
         System.out.println ("Welcome to Java");
    }
 }



Dec 21, 2012    OOSSE - Java Lecture 1             4
The Java Source File
 • A Java program can be developed using any simple editor to
   generate the Java source file
 • For example Notepad could be used
 • The source file is simple text but must be saved with an
   extension of java




Dec 21, 2012     OOSSE - Java Lecture 1           5
Compiling a Java Application
 • The Java source file is in a suitable format for humans to
   read but must be compiled into bytecode before it can be
   used
 • Sun provide a compiler called javac that is used to
   compile the source code into bytecode
 • The compiler can be invoked from a command prompt in a
   windows environment
     – javac Hello.java
 • The result of a successful compilation is a class file
   containing the bytecode
     – Hello.class




Dec 21, 2012     OOSSE - Java Lecture 1           6
Executing a Java Application
 •   The bytecode can be executed on the Java Virtual Machine
     using the interpreter provided by Sun: java Hello




 •   Note the bytecode is held in a file called Hello.class but the
     extension class is not included in the call to the interpeter:
       java Hello


Dec 21, 2012        OOSSE - Java Lecture 1               7
Java Application Development Process


       Create/ Modify Source Code         notepad




                Source Code               Hello.java



           Compile Source Code            javac Hello.java



                  Bytecode                Hello.class



               Execute Bytecode           java Hello


Dec 21, 2012     OOSSE - Java Lecture 1    8
The Java Virtual Machine
 • The bytecode produced by the Java compiler is not
   targeted at a specific machine
     – It is targeted at the Java Virtual Machine
 • The Java Runtime Environment executes the virtual
   machine
     – The Java Bytecode is executed on the virtual machine
 • Hence the bytecode is portable
     – It will execute on any machine that is running the Java
       Virtual Machine
     – The JVM needs to be target specific




Dec 21, 2012      OOSSE - Java Lecture 1             9
Pitfalls
 • Java is case sensitive
     – Hello and hello are not the same
 • All Java programs must have the java extension
 • The extension is specified when the compiler is used
     – javac Hello.java
 • The bytecode produced by the compiler is held in a file
   with a .class extension but the extension is NOT specified
   when the interpreter is used
     – java Hello




Dec 21, 2012        OOSSE - Java Lecture 1      10
Java and Classes
 • Java is an object oriented programming language
 • All code is wrapped in the form of a class:
        public class Hello
        {
        …
        }
 • Note the keywords public and class
 • The class is given a name, Hello in this case, and must be
   saved in a file called Hello.java
 • Code similar to this will appear in each of your
   applications
     – The name of the class will change

Dec 21, 2012     OOSSE - Java Lecture 1        11
The Method main
 • Classes use methods to specify what can be done
 • A main method is required in a Java application and
   defines where the application begins
 • The structure of main is fixed and will be the same in all
   applications:
   public static void main (String[] args)
   {
        …
   }
 • The syntax will be discussed in detail later
     – For now please just accept that it must be as it is



Dec 21, 2012      OOSSE - Java Lecture 1              12
The Body of main
 • The code that you write to specify what the application
   should do makes up the body of main
 • A block of code; that is one or more program statements
   wrapped in braces { }
 • For example:
    {
        System.out.println ("Hello World");
        System.out.println ("Welcome to Java");
   }
 • Note that each statement is terminated by a semicolon




Dec 21, 2012    OOSSE - Java Lecture 1        13
Simple Input – The Scanner Class




Dec 21, 2012   OOSSE - Java Lecture 1   14
Input in Java – the Scanner class
 • Java 1.5 introduces adequate support for input
     – Prior to this input from the keyboard was not trivial
 • The Scanner class can be used for keyboard input
 • Consider the following code extract:
     // build an object that knows how to obtain keyboard data
     Scanner kybd = new Scanner(System.in);
     int num1;
     // input the next integer and assign to num1
     num1 = kybd.nextInt();
 • The object kybd knows how to obtain the next integer from
   the keyboard that is identified by System.in



Dec 21, 2012      OOSSE - Java Lecture 1               15
Objects and Classes
 • Using classes makes performing complex tasks simple
 • In order to use a class you need to know:
     –   Where the class is located
     –   How to build an instance of the class – an object
     –   What instances of the class can do
     –   How to ask the instance to do something
 • A class has a set of methods that define the functionality
   that it can provide
     – For example nextInt is a method of the Scanner class
 • A method is invoked by sending a message to an object
     – num1 = kybd.nextInt();



Dec 21, 2012       OOSSE - Java Lecture 1              16
Using the Scanner Class
 • The Scanner class is contained in a Java package
     – A package is a library of classes
 • The package must be imported into an application that
   uses the Scanner class
     – So that the compiler can find it

     import java.util.*; // the package containing Scanner
     public class TestScan
     {
       public static void main (String [] args)
       {
         // build an instance of Scanner, that is an object
         Scanner kybd = new Scanner(System.in);



Dec 21, 2012      OOSSE - Java Lecture 1             17
Methods of the Scanner Class
 • Some of the methods of the Scanner class are:
     –   nextInt()      reads an integer
     –   nextDouble()   reads a double
     –   next()         reads a word
     –   nextLine()     reads the rest of the current input line


 • The two methods next and nextLine both return a value of
   type String
     – String name;
     – name = kybd.next();   // assumes kybd is an instance of
                             // the Scanner class



Dec 21, 2012      OOSSE - Java Lecture 1             18
Pitfalls
 • The nextLine method inputs the REST of a line of text
     – It starts wherever the last input finished
 • Consider the following section of code:
     Scanner kybd = new Scanner(System.in);
     String s1, s2;
     int num1 = kybd.nextInt();
     s1 = kybd.nextLine();
     s2 = kybd.nextLine();
 • What would s1 and s2 be if you entered the following?
     42
     The answer to
     Life the Universe and everything


Dec 21, 2012      OOSSE - Java Lecture 1            19
Pitfalls
 • The variable s1 would be set to an empty string
 • The variable s2 would be set to “The answer to”


 • Why?


 • What would s1 and s2 be if you entered the following?
     42 The answer to
     Life the Universe and everything




Dec 21, 2012      OOSSE - Java Lecture 1       20
Coding Style Guidelines
 • Java is a free format language but layout makes a huge
   difference to understanding
 • Adopt a good program layout to improve readability
     – Generous use of space
     – Vertical alignment of keywords
     – Indentation as appropriate
 • Use meaningful comments
     – Level of intent
     – Particularly where the code is not obvious
 • Use meaningful identifiers
 • Avoid complex program structures where possible
 • Try not to sacrifice clarity and simplicity for efficiency

Dec 21, 2012      OOSSE - Java Lecture 1            21
Summary
 In this lecture we have:
 • Introduced Java and the Java Virtual Machine
 • Discussed the basic object oriented concepts
 • Defined classes and instances
 • Reviewed the structure and syntax of a Java program
 • Introduced the Scanner class and simple I/O




Dec 21, 2012   OOSSE - Java Lecture 1        22

Mais conteúdo relacionado

Mais procurados

Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java ProgrammingRavi Kant Sahu
 
Java introduction
Java introductionJava introduction
Java introductionSagar Verma
 
Introduction to Java Programming
Introduction to Java Programming Introduction to Java Programming
Introduction to Java Programming Saravanakumar R
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java BasicsVicter Paul
 
Java Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateJava Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateAnton Keks
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: BasicsAnton Keks
 
Core java lessons
Core java lessonsCore java lessons
Core java lessonsvivek shah
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)Sujit Majety
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_javaHoang Nguyen
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Kernel Training
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java Hitesh-Java
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javajayc8586
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaAjay Sharma
 
Java Tutorial to Learn Java Programming
Java Tutorial to Learn Java ProgrammingJava Tutorial to Learn Java Programming
Java Tutorial to Learn Java Programmingbusiness Corporate
 
Java Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingJava Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingAnton Keks
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For SyntaxPravinYalameli
 

Mais procurados (20)

Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Java introduction
Java introductionJava introduction
Java introduction
 
Introduction to Java Programming
Introduction to Java Programming Introduction to Java Programming
Introduction to Java Programming
 
Introduction To Java.
Introduction To Java.Introduction To Java.
Introduction To Java.
 
Introduction to JAVA
Introduction to JAVAIntroduction to JAVA
Introduction to JAVA
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
 
Java Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateJava Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, Hibernate
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
 
Core java lessons
Core java lessonsCore java lessons
Core java lessons
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Core java
Core javaCore java
Core java
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java Tutorial to Learn Java Programming
Java Tutorial to Learn Java ProgrammingJava Tutorial to Learn Java Programming
Java Tutorial to Learn Java Programming
 
Java Course 13: JDBC & Logging
Java Course 13: JDBC & LoggingJava Course 13: JDBC & Logging
Java Course 13: JDBC & Logging
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 

Destaque

10 exceptionsin java
10 exceptionsin java10 exceptionsin java
10 exceptionsin javaAPU
 
Design patterns structuralpatterns(thedecoratorpattern)
Design patterns structuralpatterns(thedecoratorpattern)Design patterns structuralpatterns(thedecoratorpattern)
Design patterns structuralpatterns(thedecoratorpattern)APU
 
13 gui development
13 gui development13 gui development
13 gui developmentAPU
 
08 aggregation and collection classes
08  aggregation and collection classes08  aggregation and collection classes
08 aggregation and collection classesAPU
 
5. state diagrams
5. state diagrams5. state diagrams
5. state diagramsAPU
 
4. class diagrams using uml
4. class diagrams using uml4. class diagrams using uml
4. class diagrams using umlAPU
 
8. design patterns
8. design patterns8. design patterns
8. design patternsAPU
 
01 introduction to_module
01 introduction to_module01 introduction to_module
01 introduction to_moduleAPU
 
9. oo languages
9. oo languages9. oo languages
9. oo languagesAPU
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threadingAPU
 
6. activity diagrams
6. activity diagrams6. activity diagrams
6. activity diagramsAPU
 
01 introduction to_module
01 introduction to_module01 introduction to_module
01 introduction to_moduleAPU
 
4.class diagramsusinguml
4.class diagramsusinguml4.class diagramsusinguml
4.class diagramsusingumlAPU
 
09 abstract classesandinterfaces
09 abstract classesandinterfaces09 abstract classesandinterfaces
09 abstract classesandinterfacesAPU
 
7. sequence and collaboration diagrams
7. sequence and collaboration diagrams7. sequence and collaboration diagrams
7. sequence and collaboration diagramsAPU
 
3. use cases
3. use cases3. use cases
3. use casesAPU
 
Usecase diagram railway reservation system
Usecase diagram railway reservation systemUsecase diagram railway reservation system
Usecase diagram railway reservation systemmuthumeenakshim
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagramsAPU
 
14 file handling
14 file handling14 file handling
14 file handlingAPU
 

Destaque (19)

10 exceptionsin java
10 exceptionsin java10 exceptionsin java
10 exceptionsin java
 
Design patterns structuralpatterns(thedecoratorpattern)
Design patterns structuralpatterns(thedecoratorpattern)Design patterns structuralpatterns(thedecoratorpattern)
Design patterns structuralpatterns(thedecoratorpattern)
 
13 gui development
13 gui development13 gui development
13 gui development
 
08 aggregation and collection classes
08  aggregation and collection classes08  aggregation and collection classes
08 aggregation and collection classes
 
5. state diagrams
5. state diagrams5. state diagrams
5. state diagrams
 
4. class diagrams using uml
4. class diagrams using uml4. class diagrams using uml
4. class diagrams using uml
 
8. design patterns
8. design patterns8. design patterns
8. design patterns
 
01 introduction to_module
01 introduction to_module01 introduction to_module
01 introduction to_module
 
9. oo languages
9. oo languages9. oo languages
9. oo languages
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
6. activity diagrams
6. activity diagrams6. activity diagrams
6. activity diagrams
 
01 introduction to_module
01 introduction to_module01 introduction to_module
01 introduction to_module
 
4.class diagramsusinguml
4.class diagramsusinguml4.class diagramsusinguml
4.class diagramsusinguml
 
09 abstract classesandinterfaces
09 abstract classesandinterfaces09 abstract classesandinterfaces
09 abstract classesandinterfaces
 
7. sequence and collaboration diagrams
7. sequence and collaboration diagrams7. sequence and collaboration diagrams
7. sequence and collaboration diagrams
 
3. use cases
3. use cases3. use cases
3. use cases
 
Usecase diagram railway reservation system
Usecase diagram railway reservation systemUsecase diagram railway reservation system
Usecase diagram railway reservation system
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagrams
 
14 file handling
14 file handling14 file handling
14 file handling
 

Semelhante a Intro Java Lecture: Classes, I/O, JVM

OOP with Java
OOP with JavaOOP with Java
OOP with JavaOmegaHub
 
Java Standard edition(Java ) programming Basics for beginner's
Java Standard edition(Java ) programming Basics  for beginner'sJava Standard edition(Java ) programming Basics  for beginner's
Java Standard edition(Java ) programming Basics for beginner'smomin6
 
JAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptxJAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptxMurugesh33
 
JAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptxJAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptxMurugesh33
 
NDK Programming in Android
NDK Programming in AndroidNDK Programming in Android
NDK Programming in AndroidArvind Devaraj
 
OOP Lecture 1-Intro to Java.pptx
OOP Lecture 1-Intro to Java.pptxOOP Lecture 1-Intro to Java.pptx
OOP Lecture 1-Intro to Java.pptxTanzila Kehkashan
 
Elements of Java Language
Elements of Java Language Elements of Java Language
Elements of Java Language Hitesh-Java
 
Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)
Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)
Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)Mihail Stoynov
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java LanguagePawanMM
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operatorkamal kotecha
 

Semelhante a Intro Java Lecture: Classes, I/O, JVM (20)

OOP with Java
OOP with JavaOOP with Java
OOP with Java
 
Java Standard edition(Java ) programming Basics for beginner's
Java Standard edition(Java ) programming Basics  for beginner'sJava Standard edition(Java ) programming Basics  for beginner's
Java Standard edition(Java ) programming Basics for beginner's
 
JAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptxJAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptx
 
JAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptxJAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptx
 
Introduction to java technology
Introduction to java technologyIntroduction to java technology
Introduction to java technology
 
NDK Programming in Android
NDK Programming in AndroidNDK Programming in Android
NDK Programming in Android
 
Java intro
Java introJava intro
Java intro
 
1.Intro--Why Java.pptx
1.Intro--Why Java.pptx1.Intro--Why Java.pptx
1.Intro--Why Java.pptx
 
OOP Lecture 1-Intro to Java.pptx
OOP Lecture 1-Intro to Java.pptxOOP Lecture 1-Intro to Java.pptx
OOP Lecture 1-Intro to Java.pptx
 
Elements of Java Language
Elements of Java Language Elements of Java Language
Elements of Java Language
 
Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)
Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)
Modules in Java? Finally! (OpenJDK 9 Jigsaw, JSR376)
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java Language
 
Core java
Core javaCore java
Core java
 
unit1.pptx
unit1.pptxunit1.pptx
unit1.pptx
 
Introduction to oops
Introduction to oopsIntroduction to oops
Introduction to oops
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 
JAVA PPT Part-1 BY ADI.pdf
JAVA PPT Part-1 BY ADI.pdfJAVA PPT Part-1 BY ADI.pdf
JAVA PPT Part-1 BY ADI.pdf
 
JAVA PPT-1 BY ADI.pdf
JAVA PPT-1 BY ADI.pdfJAVA PPT-1 BY ADI.pdf
JAVA PPT-1 BY ADI.pdf
 
java slides
java slidesjava slides
java slides
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 

Mais de APU

. 1. introduction to object orientation
. 1. introduction to object orientation. 1. introduction to object orientation
. 1. introduction to object orientationAPU
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_moduleAPU
 
. 9. oo languages
. 9. oo languages. 9. oo languages
. 9. oo languagesAPU
 
. 8. design patterns
. 8. design patterns. 8. design patterns
. 8. design patternsAPU
 
. 5. state diagrams
. 5. state diagrams. 5. state diagrams
. 5. state diagramsAPU
 
. 4. class diagrams using uml
. 4. class diagrams using uml. 4. class diagrams using uml
. 4. class diagrams using umlAPU
 
. 2. introduction to uml
. 2. introduction to uml. 2. introduction to uml
. 2. introduction to umlAPU
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_moduleAPU
 
9.oo languages
9.oo languages9.oo languages
9.oo languagesAPU
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)APU
 

Mais de APU (10)

. 1. introduction to object orientation
. 1. introduction to object orientation. 1. introduction to object orientation
. 1. introduction to object orientation
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_module
 
. 9. oo languages
. 9. oo languages. 9. oo languages
. 9. oo languages
 
. 8. design patterns
. 8. design patterns. 8. design patterns
. 8. design patterns
 
. 5. state diagrams
. 5. state diagrams. 5. state diagrams
. 5. state diagrams
 
. 4. class diagrams using uml
. 4. class diagrams using uml. 4. class diagrams using uml
. 4. class diagrams using uml
 
. 2. introduction to uml
. 2. introduction to uml. 2. introduction to uml
. 2. introduction to uml
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_module
 
9.oo languages
9.oo languages9.oo languages
9.oo languages
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)
 

Intro Java Lecture: Classes, I/O, JVM

  • 1. Introduction to Java OOSSE Programming with Java Lecture 1 Dec 21, 2012 OOSSE - Java Lecture 1 1
  • 2. Objectives In this lecture, we will: • Introduce Java and the Java Virtual Machine • Discuss the basic object oriented concepts • Define classes and instances • Review the structure and syntax of a Java program • Introduce the Scanner class and simple I/O Dec 21, 2012 OOSSE - Java Lecture 1 2
  • 3. Introduction to Java • Java is a high level object oriented programming language • Java was designed to be: – Simple – Object oriented – Distributed – Robust – Secure – Architecture-Neutral – Portable – Multithreaded Dec 21, 2012 OOSSE - Java Lecture 1 3
  • 4. A Simple Java Application // A simple Hello World Java Application public class Hello { public static void main (String[] args) { System.out.println ("Hello World"); System.out.println ("Welcome to Java"); } } Dec 21, 2012 OOSSE - Java Lecture 1 4
  • 5. The Java Source File • A Java program can be developed using any simple editor to generate the Java source file • For example Notepad could be used • The source file is simple text but must be saved with an extension of java Dec 21, 2012 OOSSE - Java Lecture 1 5
  • 6. Compiling a Java Application • The Java source file is in a suitable format for humans to read but must be compiled into bytecode before it can be used • Sun provide a compiler called javac that is used to compile the source code into bytecode • The compiler can be invoked from a command prompt in a windows environment – javac Hello.java • The result of a successful compilation is a class file containing the bytecode – Hello.class Dec 21, 2012 OOSSE - Java Lecture 1 6
  • 7. Executing a Java Application • The bytecode can be executed on the Java Virtual Machine using the interpreter provided by Sun: java Hello • Note the bytecode is held in a file called Hello.class but the extension class is not included in the call to the interpeter: java Hello Dec 21, 2012 OOSSE - Java Lecture 1 7
  • 8. Java Application Development Process Create/ Modify Source Code notepad Source Code Hello.java Compile Source Code javac Hello.java Bytecode Hello.class Execute Bytecode java Hello Dec 21, 2012 OOSSE - Java Lecture 1 8
  • 9. The Java Virtual Machine • The bytecode produced by the Java compiler is not targeted at a specific machine – It is targeted at the Java Virtual Machine • The Java Runtime Environment executes the virtual machine – The Java Bytecode is executed on the virtual machine • Hence the bytecode is portable – It will execute on any machine that is running the Java Virtual Machine – The JVM needs to be target specific Dec 21, 2012 OOSSE - Java Lecture 1 9
  • 10. Pitfalls • Java is case sensitive – Hello and hello are not the same • All Java programs must have the java extension • The extension is specified when the compiler is used – javac Hello.java • The bytecode produced by the compiler is held in a file with a .class extension but the extension is NOT specified when the interpreter is used – java Hello Dec 21, 2012 OOSSE - Java Lecture 1 10
  • 11. Java and Classes • Java is an object oriented programming language • All code is wrapped in the form of a class: public class Hello { … } • Note the keywords public and class • The class is given a name, Hello in this case, and must be saved in a file called Hello.java • Code similar to this will appear in each of your applications – The name of the class will change Dec 21, 2012 OOSSE - Java Lecture 1 11
  • 12. The Method main • Classes use methods to specify what can be done • A main method is required in a Java application and defines where the application begins • The structure of main is fixed and will be the same in all applications: public static void main (String[] args) { … } • The syntax will be discussed in detail later – For now please just accept that it must be as it is Dec 21, 2012 OOSSE - Java Lecture 1 12
  • 13. The Body of main • The code that you write to specify what the application should do makes up the body of main • A block of code; that is one or more program statements wrapped in braces { } • For example: { System.out.println ("Hello World"); System.out.println ("Welcome to Java"); } • Note that each statement is terminated by a semicolon Dec 21, 2012 OOSSE - Java Lecture 1 13
  • 14. Simple Input – The Scanner Class Dec 21, 2012 OOSSE - Java Lecture 1 14
  • 15. Input in Java – the Scanner class • Java 1.5 introduces adequate support for input – Prior to this input from the keyboard was not trivial • The Scanner class can be used for keyboard input • Consider the following code extract: // build an object that knows how to obtain keyboard data Scanner kybd = new Scanner(System.in); int num1; // input the next integer and assign to num1 num1 = kybd.nextInt(); • The object kybd knows how to obtain the next integer from the keyboard that is identified by System.in Dec 21, 2012 OOSSE - Java Lecture 1 15
  • 16. Objects and Classes • Using classes makes performing complex tasks simple • In order to use a class you need to know: – Where the class is located – How to build an instance of the class – an object – What instances of the class can do – How to ask the instance to do something • A class has a set of methods that define the functionality that it can provide – For example nextInt is a method of the Scanner class • A method is invoked by sending a message to an object – num1 = kybd.nextInt(); Dec 21, 2012 OOSSE - Java Lecture 1 16
  • 17. Using the Scanner Class • The Scanner class is contained in a Java package – A package is a library of classes • The package must be imported into an application that uses the Scanner class – So that the compiler can find it import java.util.*; // the package containing Scanner public class TestScan { public static void main (String [] args) { // build an instance of Scanner, that is an object Scanner kybd = new Scanner(System.in); Dec 21, 2012 OOSSE - Java Lecture 1 17
  • 18. Methods of the Scanner Class • Some of the methods of the Scanner class are: – nextInt() reads an integer – nextDouble() reads a double – next() reads a word – nextLine() reads the rest of the current input line • The two methods next and nextLine both return a value of type String – String name; – name = kybd.next(); // assumes kybd is an instance of // the Scanner class Dec 21, 2012 OOSSE - Java Lecture 1 18
  • 19. Pitfalls • The nextLine method inputs the REST of a line of text – It starts wherever the last input finished • Consider the following section of code: Scanner kybd = new Scanner(System.in); String s1, s2; int num1 = kybd.nextInt(); s1 = kybd.nextLine(); s2 = kybd.nextLine(); • What would s1 and s2 be if you entered the following? 42 The answer to Life the Universe and everything Dec 21, 2012 OOSSE - Java Lecture 1 19
  • 20. Pitfalls • The variable s1 would be set to an empty string • The variable s2 would be set to “The answer to” • Why? • What would s1 and s2 be if you entered the following? 42 The answer to Life the Universe and everything Dec 21, 2012 OOSSE - Java Lecture 1 20
  • 21. Coding Style Guidelines • Java is a free format language but layout makes a huge difference to understanding • Adopt a good program layout to improve readability – Generous use of space – Vertical alignment of keywords – Indentation as appropriate • Use meaningful comments – Level of intent – Particularly where the code is not obvious • Use meaningful identifiers • Avoid complex program structures where possible • Try not to sacrifice clarity and simplicity for efficiency Dec 21, 2012 OOSSE - Java Lecture 1 21
  • 22. Summary In this lecture we have: • Introduced Java and the Java Virtual Machine • Discussed the basic object oriented concepts • Defined classes and instances • Reviewed the structure and syntax of a Java program • Introduced the Scanner class and simple I/O Dec 21, 2012 OOSSE - Java Lecture 1 22

Notas do Editor

  1. Part of this lecture will be reserved for working through solutions to selected exercises from last week. Notes relating to this do not appear in the slides.
  2. Add extra code for tracing and verification – such as conditionally compiled diagnostic code MENTION THIS LATER in the course