SlideShare una empresa de Scribd logo
1 de 45
Trinity College

                        Java Review


                     An Review
        of the Java Programming Language

                           Timothy Richards

    Trinity College, Hartford CT • Department of Computer Science • CPSC 225
What is Java?
•   Java is an object-oriented PL

•   It is statically typed
    •   Types are checked at compile-time instead of run-time

    •   Eliminates several errors before executing the code

•   It is compiled into bytecode
    •   For execution on the Java Virtual Machine

•   It is popular

•   It has LOTS of libraries

    Trinity College, Hartford CT • Department of Computer Science • CPSC 225
Smallest Java Program

      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }


         Everything in Java is contained
         within a class declaration.




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   3
Smallest Java Program

      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }


         To make a class executable, you
         need a special method called main




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   4
Smallest Java Program

      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }


         The main method is also a static method.
         A static method is associated with a class
         not an instance.




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   5
Smallest Java Program

      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }


         Java has arrays. Java has Strings. In Java,
         you must specify the type of things.

         String[] => an array of String objects



Trinity College, Hartford CT • Department of Computer Science • CPSC 225   6
Smallest Java Program

      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }


         void is also a type. It represents the
         absence of a type. It can only be used to
         indicate that a method returns no value.




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   7
Smallest Java Program

      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }


         So, how do we “run” this program?




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   8
Smallest Java Program

      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }


         So, how do we “run” this program?
         It is a 2-step process:
         1. Compile the program (javac)
         2. Run the program (java)



Trinity College, Hartford CT • Department of Computer Science • CPSC 225   9
Smallest Java Program
                                                      Hello.java
      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }




                                 javac




                               Hello.class             java                output


Trinity College, Hartford CT • Department of Computer Science • CPSC 225            10
Smallest Java Program
                                                      Hello.java
      public class Hello {
        public static void main(String[] args) {
          System.out.println(“Hello, World”);
        }
      }




                                 javac
 Let’s Give it
    A Try!
                               Hello.class             java                output


Trinity College, Hartford CT • Department of Computer Science • CPSC 225            11
Java Types
•   Primitive Types
    •   boolean, byte, char, short, int, long, double

•   Reference Types
    •   Boolean, Integer, Character, ...

    •   String

    •   User-defined Classes

•   Arrays
    •   Multi-dimensional


    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   12
Java Classes
•   Have a name

•   Have visibility
    •   default, public, private, protected

•   Have instance variables

•   Have methods

•   Have class variables

•   Have class methods



    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   13
Java Classes
      public class Hello {

      }




                 This class has a name



Trinity College, Hartford CT • Department of Computer Science • CPSC 225   14
Java Classes
      public class Hello {

      }




                 It is a “public” class.
            It has package-level visibility.


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   15
Java Classes
      public class Hello {
        private String name;
        private int    age;
      }




           These are instance variables.
            Note, they are “private”.


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   16
Java Classes
      public class Hello {
        private String name;
        private int    age;

          public String name() {
            return name;
          }

          public int age() {
            return int;
          }
      }




            These are instance methods.
              Note, they are “public”.


Trinity College, Hartford CT • Department of Computer Science • CPSC 225   17
Java Classes
      public class Hello {
        private String name;
        private int    age;

          public Hello(String name, int age) {
            this.name = name;
            this.age = age;
          }

          public String name() {
            return name;
          }

          public int age() {
            return int;
          }
      }


       This is a constructor for class Hello
Trinity College, Hartford CT • Department of Computer Science • CPSC 225   18
Java Classes
public class Hello {                                public class Main {
                                                      public static void main(String[] args)
  private String name;                                {
  private int    age;
                                                        }
    public Hello(String name, int age) {            }
      this.name = name;
      this.age = age;
    }

    public String name() {
      return name;
    }

    public int age() {
      return int;
    }
}


                Let’s create a new class called Main...
           Trinity College, Hartford CT • Department of Computer Science • CPSC 225       19
Java Classes
public class Hello {                                public class Main {
                                                      public static void main(String[] args)
  private String name;                                {
  private int    age;                                   Hello h1 = new Hello(“tim”, 34);
                                                        Hello h2 = new Hello(“sue”, 79);
    public Hello(String name, int age) {              }
                                                    }
      this.name = name;
      this.age = age;
    }

    public String name() {
      return name;
    }

    public int age() {
      return int;
    }
}


              This is how you “create” new objects...
           Trinity College, Hartford CT • Department of Computer Science • CPSC 225       20
Java Classes
public class Hello {                                public class Main {
                                                      public static void main(String[] args)
  private String name;                                {
  private int    age;                                   Hello h1 = new Hello(“tim”, 34);
                                                        Hello h2 = new Hello(“sue”, 79);
    public Hello(String name, int age) {              }
                                                    }
      this.name = name;
      this.age = age;
    }
                                                               h1               h2
    public String name() {
      return name;
    }
                                                       “tim”        34    “sue”       79
    public int age() {
      return int;
    }
}


              This is how you “create” new objects...
           Trinity College, Hartford CT • Department of Computer Science • CPSC 225        21
Java Classes
public class Hello {                                public class Main {
                                                      public static void main(String[] args)
  private String name;                                {
  private int    age;                                   Hello h1 = new Hello(“tim”, 34);
                                                        Hello h2 = new Hello(“sue”, 79);
    public Hello(String name, int age) {
                                                            System.out.println(h1.name());
      this.name = name;                                     System.out.println(h1.age());
      this.age = age;                                       System.out.println(h2.name());
    }                                                       System.out.println(h2.age());
                                                        }
                                                    }
    public String name() {
      return name;
    }

    public int age() {
      return int;
    }
}


               We can print their contents by calling
                      their public methods
           Trinity College, Hartford CT • Department of Computer Science • CPSC 225          22
Java Classes
public class Hello {                                public class Main {
                                                      public static void main(String[] args)
  private String name;                                {
  private int    age;                                   Hello h1 = new Hello(“tim”, 34);
                                                        Hello h2 = new Hello(“sue”, 79);
    public Hello(String name, int age) {
                                                            System.out.println(h1.name());
      this.name = name;                                     System.out.println(h1.age());
      this.age = age;                                       System.out.println(h2.name());
    }                                                       System.out.println(h2.age());
                                                        }
                                                    }
    ...

    public toString() {
      return “Hello(” + name + “ “ +
             age + “)”
    }
}



            Or, we could include a toString method...
           Trinity College, Hartford CT • Department of Computer Science • CPSC 225          23
Java Classes
public class Hello {                                public class Main {
                                                      public static void main(String[] args)
  private String name;                                {
  private int    age;                                   Hello h1 = new Hello(“tim”, 34);
                                                        Hello h2 = new Hello(“sue”, 79);
    public Hello(String name, int age) {
                                                            System.out.println(h1);
      this.name = name;                                     System.out.println(h2);
      this.age = age;                                   }
    }                                               }

    ...

    public toString() {
      return “Hello(” + name + “ “ +
             age + “)”
    }
}



                       And simply print the object
           Trinity College, Hartford CT • Department of Computer Science • CPSC 225       24
Java Classes
public class Hello {                                public class Main {
                                                      public static void main(String[] args)
  private String name;                                {
  private int    age;                                   Hello h1 = new Hello(“tim”, 34);
                                                        Hello h2 = new Hello(“sue”, 79);
    public Hello(String name, int age) {
                                                            System.out.println(h1);
      this.name = name;                                     System.out.println(h2);
      this.age = age;                                   }
    }                                               }

    ...

    public toString() {
      return “Hello(” + name + “ “ +
             age + “)”
    }
}



                                    Let’s Try It!
           Trinity College, Hartford CT • Department of Computer Science • CPSC 225       25
Java Control
•   Making choices
    •   if-then-else

    •   switch

•   Looping
    •   for loop

    •   while loop




    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   26
Java Control - if-else
              public class Main {
                public static void main(String[] args)
                {
                  // Here is a comment
                  /* Here is also a comment
                     on several lines */

                      // declare a variable
                      int val = 5;

                      // check its value
                      if (val == 5)
                        System.out.println(“five”);
                  }
              }




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   27
Java Control - if-else
              public class Main {
                public static void main(String[] args)
                {
                  // Here is a comment
                  /* Here is also a comment
                     on several lines */

                      // declare a variable
                      int val = 5;

                      // check its value
                      if (val == 5)
                        System.out.println(“five”);
                      else
                        System.out.println(“not sure”);
                  }
              }




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   28
Java Control - if-else
              public class Main {
                public static void main(String[] args)
                {
                  // Here is a comment
                  /* Here is also a comment
                     on several lines */

                      // declare a variable
                      int val = 5;

                      // check its value
                      if (val == 5) {
                        System.out.println(“five”);
                      }
                      else {
                        System.out.println(“not sure”);
                      }
                  }
              }




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   29
Java Control - switch
              public class Main {
                public static void main(String[] args)
                {
                  // Here is a comment
                  /* Here is also a comment
                     on several lines */

                      // declare a variable
                      int val = 5;

                      // check its value
                      switch (val) {
                      case 3:
                        System.out.println(“three”);
                        break;
                      case 4:
                        System.out.println(“four”);
                        break;
                      case 5:
                        System.out.println(“five”);
                        break;
                      default:
                        system.out.println(“not sure”);
                      }
                  }
              }



Trinity College, Hartford CT • Department of Computer Science • CPSC 225   30
Java Control - for loop
              public class Main {
                public static void main(String[] args)
                {
                  // Here is a comment
                  /* Here is also a comment
                     on several lines */

                      // declare a variable
                      int val = 5;

                      for (int i = 0; i < val; i++) {
                        System.out.println(i);
                      }
                  }
              }




Trinity College, Hartford CT • Department of Computer Science • CPSC 225   31
Java Control - while loop
              public class Main {
                public static void main(String[] args)
                {
                  // Here is a comment
                  /* Here is also a comment
                     on several lines */

                      // declare a variable
                      int val = 5;

                      while (val != 0) {
                        System.out.println(val);
                        val--;
                                                             Let’s Try It!
                      }
                  }
              }




Trinity College, Hartford CT • Department of Computer Science • CPSC 225     32
Java IO
•   Input/Output

•   Java Provides Streams
    •   Streams can be layered

    •   Each layer provides a “service”

•   Three Standard Streams (static)
    •   System.out

    •   System.in

    •   System.err


    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   33
Java IO
              public class Main {
                public static void main(String[] args)    System.out is a object of
                {                                         type PrintStream.
                  // We have seen System.out:
                  System.out.println(“hello”);
                }                                         It provides many
              }                                           convenient methods for
                                                          printing data values
                                                          conveniently




Trinity College, Hartford CT • Department of Computer Science • CPSC 225          34
Java IO
                            public class Main {
System.in is an object of     public static void main(String[] args)   System.out is a object of
type InputStream. It          {                                        type PrintStream.
                                // We have seen System.out:
provides methods for            System.out.println(“hello”);
reading bytes.                                                         It provides many
                                    // Reading input:                  convenient methods for
                                    int val = System.in.read();
                                }                                      printing data values
                            }                                          conveniently




            Trinity College, Hartford CT • Department of Computer Science • CPSC 225           35
Java IO
                            public class Main {
System.in is an object of     public static void main(String[] args)   System.out is a object of
type InputStream. It          {                                        type PrintStream.
                                // We have seen System.out:
provides methods for            System.out.println(“hello”);
reading bytes.                                                         It provides many
                                    // Reading input:                  convenient methods for
                                    int val = System.in.read();
How do I read a                 }                                      printing data values
line of input?              }                                          conveniently




            Trinity College, Hartford CT • Department of Computer Science • CPSC 225           36
Java IO
                            public class Main {
System.in is an object of     public static void main(String[] args)      System.out is a object of
type InputStream. It          {                                           type PrintStream.
                                // We have seen System.out:
provides methods for            System.out.println(“hello”);
reading bytes.                                                            It provides many
                                    // Reading input:                     convenient methods for
                                    int val = System.in.read();
How do I read a                                                           printing data values
line of input?                      InputStreamReader is =                conveniently
                                      new InputStreamReader(System.in);
                                    BufferedReader br =
You need to wrap                      new BufferedReader(is);
System.in with an                   String line = br.readLine();
InputStreamReader
                                    System.out.println(line);
and then a                      }
BufferedReader!             }




             Trinity College, Hartford CT • Department of Computer Science • CPSC 225             37
Java IO
                            public class Main {
System.in is an object of     public static void main(String[] args)      System.out is a object of
type InputStream. It          {                                           type PrintStream.
                                // We have seen System.out:
provides methods for            System.out.println(“hello”);
reading bytes.                                                            It provides many
                                    // Reading input:                     convenient methods for
                                    int val = System.in.read();
How do I read a                                                           printing data values
line of input?                      InputStreamReader is =                conveniently
                                      new InputStreamReader(System.in);
                                    BufferedReader br =
You need to wrap                      new BufferedReader(is);
System.in with an                   String line = br.readLine();
InputStreamReader
                                    System.out.println(line);
and then a                      }
BufferedReader!             }


Turns out this is not
quite right...

Let’s give it a try
and see...
             Trinity College, Hartford CT • Department of Computer Science • CPSC 225             38
Exceptions
•   Represent exceptional conditions

•   A class can throw exceptions
    •   Indicated as part of a method definition

•   Exceptions can be caught
    •   Using a try-catch block




    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   39
Exceptions
                           import java.io.InputStreamReader;
Easy way:                  import java.io.BufferedReader;
have someone else
                           public class Main {
handle the problem!          public static void main(String[] args) throws IOException
                             {
                               // We have seen System.out:
                               System.out.println(“hello”);

                                   // Reading input:
                                   int val = System.in.read();

                                   InputStreamReader is =
                                     new InputStreamReader(System.in);
                                   BufferedReader br =
                                     new BufferedReader(is);

                                   String line = br.readLine();

                                   System.out.println(line);
                               }
                           }




           Trinity College, Hartford CT • Department of Computer Science • CPSC 225      40
Exceptions
                            import java.io.InputStreamReader;
Easy way:                   import java.io.BufferedReader;
have someone else
                            public class Main {
handle the problem!           public static void main(String[] args)
                              {
Hard way:                       // We have seen System.out:
                                System.out.println(“hello”);
Deal with the problem
using a try-catch block           try {
                                    // Reading input:
                                    int val = System.in.read();

                                   InputStreamReader is =
                                     new InputStreamReader(System.in);
                                   BufferedReader br =
                                     new BufferedReader(is);

                                   String line = br.readLine();

                                   System.out.println(line);
                                  }
                                  catch (IOException e) {
                                    System.out.println(e);
                                  }
                              }



            Trinity College, Hartford CT • Department of Computer Science • CPSC 225   41
Packages
•   Convenient mechanism to group related
    classes

•   A package is defined in two parts:
    •   A java file begins with a package declaration

    •   That same java file exists in a directory structure that
        mirrors the package name

•   Example
    •   package: cpsc225.example

    •   directory: cpsc225/example/Hello.java

    Trinity College, Hartford CT • Department of Computer Science • CPSC 225   42
Packages
    cpsc225/example                  cpsc225/main                     cpsc225/util



        Foo.java                       Bar.java                         Baz.java


public class Foo {        import cpsc225.example.Foo;               public class Baz {
  ...                     import cpsc225.util.Baz                     ...
}                                                                   }
                          public class Bar {
                            public static void main
                            (String[] args) {
                              Foo f1 = new Foo();
                              Baz b1 = new Baz();


                                  cpsc225.example.Foo f2 =
                                   new cpsc225.example.Foo();

                                  cpsc225.util.Baz b2 =
                                   new cpsc225.util.Baz();
                              }
                          }


          Trinity College, Hartford CT • Department of Computer Science • CPSC 225       43
Packages
    cpsc225/example                   cpsc225/main                    cpsc225/util



        Foo.java                        Bar.java                        Baz.java
package cpsc225.example;   package cpsc225.main;                    package cpsc225.util;

public class Foo {         import cpsc225.example.Foo;              public class Baz {
  ...                      import cpsc225.util.Baz                    ...
}                                                                   }
                           public class Bar {
                             public static void main
                             (String[] args) {
                               Foo f1 = new Foo();
                               Baz b1 = new Baz();
                                                                   Let’s Try It!
                                   cpsc225.example.Foo f2 =
                                    new cpsc225.example.Foo();

                                   cpsc225.util.Baz b2 =
                                    new cpsc225.util.Baz();
                               }
                           }

          Trinity College, Hartford CT • Department of Computer Science • CPSC 225          44
Hands-On Activity


$ git clone git://github.com/timdrichards/java-review.git

                                 cd lab-01




     Trinity College, Hartford CT • Department of Computer Science • CPSC 225   45

Más contenido relacionado

La actualidad más candente (9)

Chapter06 designing class
Chapter06 designing classChapter06 designing class
Chapter06 designing class
 
Polymorphism 9
Polymorphism 9Polymorphism 9
Polymorphism 9
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of features
 
Excuse me, sir, do you have a moment to talk about tests in Kotlin
Excuse me, sir, do you have a moment to talk about tests in KotlinExcuse me, sir, do you have a moment to talk about tests in Kotlin
Excuse me, sir, do you have a moment to talk about tests in Kotlin
 
Java session13
Java session13Java session13
Java session13
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
ThreadProperties
ThreadPropertiesThreadProperties
ThreadProperties
 
Ios development
Ios developmentIos development
Ios development
 

Similar a java review

Java class 3
Java class 3Java class 3
Java class 3
Edureka!
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
shatha00
 

Similar a java review (20)

Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
7 inheritance
7 inheritance7 inheritance
7 inheritance
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
 
Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8 Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8
 
final year project center in Coimbatore
final year project center in Coimbatorefinal year project center in Coimbatore
final year project center in Coimbatore
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
Java class 3
Java class 3Java class 3
Java class 3
 
Core java
Core javaCore java
Core java
 
Abstraction
AbstractionAbstraction
Abstraction
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
11slide
11slide11slide
11slide
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 

Más de University of Massachusetts Amherst (7)

Community College Day Spring 2013
Community College Day Spring 2013Community College Day Spring 2013
Community College Day Spring 2013
 
Freshmen Advising Spring 2013
Freshmen Advising Spring 2013Freshmen Advising Spring 2013
Freshmen Advising Spring 2013
 
Basic SQL Part 2
Basic SQL Part 2Basic SQL Part 2
Basic SQL Part 2
 
Markup Languages
Markup LanguagesMarkup Languages
Markup Languages
 
Lecture 07 - Basic SQL
Lecture 07 - Basic SQLLecture 07 - Basic SQL
Lecture 07 - Basic SQL
 
Java review-2
Java review-2Java review-2
Java review-2
 
Lecture 06
Lecture 06Lecture 06
Lecture 06
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Último (20)

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 

java review

  • 1. Trinity College Java Review An Review of the Java Programming Language Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 225
  • 2. What is Java? • Java is an object-oriented PL • It is statically typed • Types are checked at compile-time instead of run-time • Eliminates several errors before executing the code • It is compiled into bytecode • For execution on the Java Virtual Machine • It is popular • It has LOTS of libraries Trinity College, Hartford CT • Department of Computer Science • CPSC 225
  • 3. Smallest Java Program public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } Everything in Java is contained within a class declaration. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 3
  • 4. Smallest Java Program public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } To make a class executable, you need a special method called main Trinity College, Hartford CT • Department of Computer Science • CPSC 225 4
  • 5. Smallest Java Program public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } The main method is also a static method. A static method is associated with a class not an instance. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 5
  • 6. Smallest Java Program public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } Java has arrays. Java has Strings. In Java, you must specify the type of things. String[] => an array of String objects Trinity College, Hartford CT • Department of Computer Science • CPSC 225 6
  • 7. Smallest Java Program public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } void is also a type. It represents the absence of a type. It can only be used to indicate that a method returns no value. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 7
  • 8. Smallest Java Program public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } So, how do we “run” this program? Trinity College, Hartford CT • Department of Computer Science • CPSC 225 8
  • 9. Smallest Java Program public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } So, how do we “run” this program? It is a 2-step process: 1. Compile the program (javac) 2. Run the program (java) Trinity College, Hartford CT • Department of Computer Science • CPSC 225 9
  • 10. Smallest Java Program Hello.java public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } javac Hello.class java output Trinity College, Hartford CT • Department of Computer Science • CPSC 225 10
  • 11. Smallest Java Program Hello.java public class Hello { public static void main(String[] args) { System.out.println(“Hello, World”); } } javac Let’s Give it A Try! Hello.class java output Trinity College, Hartford CT • Department of Computer Science • CPSC 225 11
  • 12. Java Types • Primitive Types • boolean, byte, char, short, int, long, double • Reference Types • Boolean, Integer, Character, ... • String • User-defined Classes • Arrays • Multi-dimensional Trinity College, Hartford CT • Department of Computer Science • CPSC 225 12
  • 13. Java Classes • Have a name • Have visibility • default, public, private, protected • Have instance variables • Have methods • Have class variables • Have class methods Trinity College, Hartford CT • Department of Computer Science • CPSC 225 13
  • 14. Java Classes public class Hello { } This class has a name Trinity College, Hartford CT • Department of Computer Science • CPSC 225 14
  • 15. Java Classes public class Hello { } It is a “public” class. It has package-level visibility. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 15
  • 16. Java Classes public class Hello { private String name; private int age; } These are instance variables. Note, they are “private”. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 16
  • 17. Java Classes public class Hello { private String name; private int age; public String name() { return name; } public int age() { return int; } } These are instance methods. Note, they are “public”. Trinity College, Hartford CT • Department of Computer Science • CPSC 225 17
  • 18. Java Classes public class Hello { private String name; private int age; public Hello(String name, int age) { this.name = name; this.age = age; } public String name() { return name; } public int age() { return int; } } This is a constructor for class Hello Trinity College, Hartford CT • Department of Computer Science • CPSC 225 18
  • 19. Java Classes public class Hello { public class Main { public static void main(String[] args) private String name; { private int age; } public Hello(String name, int age) { } this.name = name; this.age = age; } public String name() { return name; } public int age() { return int; } } Let’s create a new class called Main... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 19
  • 20. Java Classes public class Hello { public class Main { public static void main(String[] args) private String name; { private int age; Hello h1 = new Hello(“tim”, 34); Hello h2 = new Hello(“sue”, 79); public Hello(String name, int age) { } } this.name = name; this.age = age; } public String name() { return name; } public int age() { return int; } } This is how you “create” new objects... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 20
  • 21. Java Classes public class Hello { public class Main { public static void main(String[] args) private String name; { private int age; Hello h1 = new Hello(“tim”, 34); Hello h2 = new Hello(“sue”, 79); public Hello(String name, int age) { } } this.name = name; this.age = age; } h1 h2 public String name() { return name; } “tim” 34 “sue” 79 public int age() { return int; } } This is how you “create” new objects... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 21
  • 22. Java Classes public class Hello { public class Main { public static void main(String[] args) private String name; { private int age; Hello h1 = new Hello(“tim”, 34); Hello h2 = new Hello(“sue”, 79); public Hello(String name, int age) { System.out.println(h1.name()); this.name = name; System.out.println(h1.age()); this.age = age; System.out.println(h2.name()); } System.out.println(h2.age()); } } public String name() { return name; } public int age() { return int; } } We can print their contents by calling their public methods Trinity College, Hartford CT • Department of Computer Science • CPSC 225 22
  • 23. Java Classes public class Hello { public class Main { public static void main(String[] args) private String name; { private int age; Hello h1 = new Hello(“tim”, 34); Hello h2 = new Hello(“sue”, 79); public Hello(String name, int age) { System.out.println(h1.name()); this.name = name; System.out.println(h1.age()); this.age = age; System.out.println(h2.name()); } System.out.println(h2.age()); } } ... public toString() { return “Hello(” + name + “ “ + age + “)” } } Or, we could include a toString method... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 23
  • 24. Java Classes public class Hello { public class Main { public static void main(String[] args) private String name; { private int age; Hello h1 = new Hello(“tim”, 34); Hello h2 = new Hello(“sue”, 79); public Hello(String name, int age) { System.out.println(h1); this.name = name; System.out.println(h2); this.age = age; } } } ... public toString() { return “Hello(” + name + “ “ + age + “)” } } And simply print the object Trinity College, Hartford CT • Department of Computer Science • CPSC 225 24
  • 25. Java Classes public class Hello { public class Main { public static void main(String[] args) private String name; { private int age; Hello h1 = new Hello(“tim”, 34); Hello h2 = new Hello(“sue”, 79); public Hello(String name, int age) { System.out.println(h1); this.name = name; System.out.println(h2); this.age = age; } } } ... public toString() { return “Hello(” + name + “ “ + age + “)” } } Let’s Try It! Trinity College, Hartford CT • Department of Computer Science • CPSC 225 25
  • 26. Java Control • Making choices • if-then-else • switch • Looping • for loop • while loop Trinity College, Hartford CT • Department of Computer Science • CPSC 225 26
  • 27. Java Control - if-else public class Main { public static void main(String[] args) { // Here is a comment /* Here is also a comment on several lines */ // declare a variable int val = 5; // check its value if (val == 5) System.out.println(“five”); } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 27
  • 28. Java Control - if-else public class Main { public static void main(String[] args) { // Here is a comment /* Here is also a comment on several lines */ // declare a variable int val = 5; // check its value if (val == 5) System.out.println(“five”); else System.out.println(“not sure”); } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 28
  • 29. Java Control - if-else public class Main { public static void main(String[] args) { // Here is a comment /* Here is also a comment on several lines */ // declare a variable int val = 5; // check its value if (val == 5) { System.out.println(“five”); } else { System.out.println(“not sure”); } } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 29
  • 30. Java Control - switch public class Main { public static void main(String[] args) { // Here is a comment /* Here is also a comment on several lines */ // declare a variable int val = 5; // check its value switch (val) { case 3: System.out.println(“three”); break; case 4: System.out.println(“four”); break; case 5: System.out.println(“five”); break; default: system.out.println(“not sure”); } } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 30
  • 31. Java Control - for loop public class Main { public static void main(String[] args) { // Here is a comment /* Here is also a comment on several lines */ // declare a variable int val = 5; for (int i = 0; i < val; i++) { System.out.println(i); } } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 31
  • 32. Java Control - while loop public class Main { public static void main(String[] args) { // Here is a comment /* Here is also a comment on several lines */ // declare a variable int val = 5; while (val != 0) { System.out.println(val); val--; Let’s Try It! } } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 32
  • 33. Java IO • Input/Output • Java Provides Streams • Streams can be layered • Each layer provides a “service” • Three Standard Streams (static) • System.out • System.in • System.err Trinity College, Hartford CT • Department of Computer Science • CPSC 225 33
  • 34. Java IO public class Main { public static void main(String[] args) System.out is a object of { type PrintStream. // We have seen System.out: System.out.println(“hello”); } It provides many } convenient methods for printing data values conveniently Trinity College, Hartford CT • Department of Computer Science • CPSC 225 34
  • 35. Java IO public class Main { System.in is an object of public static void main(String[] args) System.out is a object of type InputStream. It { type PrintStream. // We have seen System.out: provides methods for System.out.println(“hello”); reading bytes. It provides many // Reading input: convenient methods for int val = System.in.read(); } printing data values } conveniently Trinity College, Hartford CT • Department of Computer Science • CPSC 225 35
  • 36. Java IO public class Main { System.in is an object of public static void main(String[] args) System.out is a object of type InputStream. It { type PrintStream. // We have seen System.out: provides methods for System.out.println(“hello”); reading bytes. It provides many // Reading input: convenient methods for int val = System.in.read(); How do I read a } printing data values line of input? } conveniently Trinity College, Hartford CT • Department of Computer Science • CPSC 225 36
  • 37. Java IO public class Main { System.in is an object of public static void main(String[] args) System.out is a object of type InputStream. It { type PrintStream. // We have seen System.out: provides methods for System.out.println(“hello”); reading bytes. It provides many // Reading input: convenient methods for int val = System.in.read(); How do I read a printing data values line of input? InputStreamReader is = conveniently new InputStreamReader(System.in); BufferedReader br = You need to wrap new BufferedReader(is); System.in with an String line = br.readLine(); InputStreamReader System.out.println(line); and then a } BufferedReader! } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 37
  • 38. Java IO public class Main { System.in is an object of public static void main(String[] args) System.out is a object of type InputStream. It { type PrintStream. // We have seen System.out: provides methods for System.out.println(“hello”); reading bytes. It provides many // Reading input: convenient methods for int val = System.in.read(); How do I read a printing data values line of input? InputStreamReader is = conveniently new InputStreamReader(System.in); BufferedReader br = You need to wrap new BufferedReader(is); System.in with an String line = br.readLine(); InputStreamReader System.out.println(line); and then a } BufferedReader! } Turns out this is not quite right... Let’s give it a try and see... Trinity College, Hartford CT • Department of Computer Science • CPSC 225 38
  • 39. Exceptions • Represent exceptional conditions • A class can throw exceptions • Indicated as part of a method definition • Exceptions can be caught • Using a try-catch block Trinity College, Hartford CT • Department of Computer Science • CPSC 225 39
  • 40. Exceptions import java.io.InputStreamReader; Easy way: import java.io.BufferedReader; have someone else public class Main { handle the problem! public static void main(String[] args) throws IOException { // We have seen System.out: System.out.println(“hello”); // Reading input: int val = System.in.read(); InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); String line = br.readLine(); System.out.println(line); } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 40
  • 41. Exceptions import java.io.InputStreamReader; Easy way: import java.io.BufferedReader; have someone else public class Main { handle the problem! public static void main(String[] args) { Hard way: // We have seen System.out: System.out.println(“hello”); Deal with the problem using a try-catch block try { // Reading input: int val = System.in.read(); InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); String line = br.readLine(); System.out.println(line); } catch (IOException e) { System.out.println(e); } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 41
  • 42. Packages • Convenient mechanism to group related classes • A package is defined in two parts: • A java file begins with a package declaration • That same java file exists in a directory structure that mirrors the package name • Example • package: cpsc225.example • directory: cpsc225/example/Hello.java Trinity College, Hartford CT • Department of Computer Science • CPSC 225 42
  • 43. Packages cpsc225/example cpsc225/main cpsc225/util Foo.java Bar.java Baz.java public class Foo { import cpsc225.example.Foo; public class Baz { ... import cpsc225.util.Baz ... } } public class Bar { public static void main (String[] args) { Foo f1 = new Foo(); Baz b1 = new Baz(); cpsc225.example.Foo f2 = new cpsc225.example.Foo(); cpsc225.util.Baz b2 = new cpsc225.util.Baz(); } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 43
  • 44. Packages cpsc225/example cpsc225/main cpsc225/util Foo.java Bar.java Baz.java package cpsc225.example; package cpsc225.main; package cpsc225.util; public class Foo { import cpsc225.example.Foo; public class Baz { ... import cpsc225.util.Baz ... } } public class Bar { public static void main (String[] args) { Foo f1 = new Foo(); Baz b1 = new Baz(); Let’s Try It! cpsc225.example.Foo f2 = new cpsc225.example.Foo(); cpsc225.util.Baz b2 = new cpsc225.util.Baz(); } } Trinity College, Hartford CT • Department of Computer Science • CPSC 225 44
  • 45. Hands-On Activity $ git clone git://github.com/timdrichards/java-review.git cd lab-01 Trinity College, Hartford CT • Department of Computer Science • CPSC 225 45

Notas del editor