1 / 17

Java

Java. Getting Started. "Hello, World" Application. public class Hello { public static void main (String args[ ]) { System.out.println ("Hello, World!"); } }. Memorize this: public static void main (String args[ ]) Use System.out.println for output.

rafer
Download Presentation

Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Getting Started

  2. "Hello, World" Application public class Hello { public static void main (String args[ ]) { System.out.println ("Hello, World!"); } } • Memorize this: public static void main (String args[ ]) • Use System.out.println for output

  3. Compiling a Java program • The program defined public class Hello • Therefore, the file must be named Hello.java • Compile the program with javac Hello.java • This creates a file named Hello.class • Hello.class contains Java byte code • Run the program with java Hello • This runs the Java virtual machine (interpreter)

  4. "Hi, World" Applet import java.applet.*; import java.awt.*; public class HiWorld extends Applet { public void paint (Graphics g) { g.drawString ("Hi, world!", 50, 100); } }

  5. No main method • Yes, applets have a main method... • ...but it's in the browser, not in your code!

  6. import Statements • import java.applet.*; makes available all the classes and objects in the java.applet package. • import is not #include -- it doesn't make the program bigger, it just makes things easier to reference. • Without importjava.applet.*; you could still say java.applet.paint (...)instead of paint (...)

  7. The paint method • public void paint (Graphics g) { g.drawString ("Hi, world!", 50, 100); } • This is a method you supply in class HiWorld • It overrides the predefined paint (Graphics) method in Applet • If you don't define this method, you get the inherited method (which does nothing!)

  8. "Hi, World" Applet (repeat) import java.applet.*; import java.awt.*; public class HiWorld extends Applet { public void paint (Graphics g) { g.drawString ("Hi, world!", 50, 100); } }

  9. The Person class class Person { String name; int age = 20; void birthday ( ) { age++; System.out.println (name + " is now "+ age); } }

  10. The People program public class People { public static void main (String args [ ]) { Person john; // "People" uses "Person" john = new Person ( ); Person mary = new Person ( ); john.name = "John Doe"; john.birthday ( ); } }

  11. Sending a message • You don't "call a function," you send a message to an object • The object may execute one of its own methods • The object may execute an inherited method • You generally need a pretty clear idea of what methods are being inherited

  12. toString • toString( ) is defined at Object, and is therefore inherited by every object • System.out.println, when given any object, automatically calls that object's toString method • The resultant output is better than nothing • It's a good idea to define toString( ) for every class you write

  13. An example toString( ) method • Returns something like "John, age 34" class Person { String name; int age; public String toString ( ) { return name + ", age " + age;}

  14. Parameter transmission • In Java, • primitives (int, double, char, boolean, etc.) are passed by value • objects (Person, String, arrays, etc.) are passed by reference • This rule is simple and easy to work with • You never accidentally get a copy of an object • If you need to copy an object, do it "by hand"

  15. Procedural thinking • Procedural programs use functional decomposition • Decide what program is to do • Write a top level procedure, inventing new lower-level procedures as you go • Write lower-level procedures • Continue until program is fully coded • Goal: small, relatively independent parts

  16. Object thinking • Decide what program is to do • Decide what objects you need • For each object, decide • what are its responsibilities • with which other objects it must collaborate • Goal: small, relatively independent parts

  17. The End

More Related