A Crash Course in Java Redirected from Bachelor/Year 2/Object Oriented Modelling/Summary/Page Title

Page last modified 14:25, 16 Dec 2008 by Joren | Page History

"Hello, World!" in Java

public class Greeter{

   private String name;

   public Greeter(String aName){
      name = aName;
   } 

   public String sayHello(){
      return "Hello, " + name + "!";
   }
}

constructor: Greeter(String aName)
method: sayHello()
field: private String name

public class GreeterTester
{
   public static void main(String[] args)
   {
      Greeter worldGreeter = new Greeter("World");
      String greeting = worldGreeter.sayHello();
      System.out.println(greeting);
   }
}

this class contains a main method, which means its runnable.
static: it doesn't operate on objects.
args: command-line arguments

If you want to run your program in a Shell Window:
    go to your folder of your program,
    javac filename.java => Run compiles
    java filename => Start interpreter
    output...

Documentation comments

/**
   A class for producing simple greetings.
*/
 
public class Greeter {
   /**
      Constructs a Greeter object that can greet a person or 
      entity.
      @param aName the name of the person or entity who should
      be addressed in the greetings.
   */
   public Greeter(String aName) {
      name = aName;
   }
 
   /**
      Greet with a "Hello" message.
      @return a message containing "Hello" and the name of
      the greeted person or entity.
   */
   public String sayHello() {
      return "Hello, " + name + "!";
   }
 
   private String name;
}

JavaDoc.png

JavaDoc2.png

Primitive Types

The primitive Types of the Java Language

Type Size Range
int 4 bytes -2,147,483,648 ... 2,147,483,647
long 8 bytes

-9,233,372,036,854,775,808L ...
9,233,372,036,854,775,807 L   

short 2 bytes  -32768 ... 32767
byte 1 byte  -128 ... 127
char 2 bytes  '\u0000' ... '\uFFFF'
boolean   false, true
double 8 bytes approximately ±1.79769313486231570E+308
float 4 bytes approximately ±3.40282347E+38F

 Character Escape Sequences

Escape Sequence Meaning
\b backspace (\u0008)
\f form feed (\u000C)
\n newline (\u000A)
\r return (\u000D)
\t tab (\u0009)
\\ backslash
\' single quote
\" double quote
\un1n2n3n4 Unicode encoding

Mathematical Methods

Method Description
Math.sqrt(x) Square root of x, √x
Math.pow(x, y) xy (x > 0, ot x = 0 and y > 0, or x < 0 and y is an integer)
Math.toRadians(x) Converts x degrees to radians (i.e., returns x . π/180)
Math.toDegrees(x) Converts x radians to degrees (i.e., returns x . 180/π)
Math.round(x) Closest integer to x (as a long)
Math.abs(x) Absolute value |x|

Object References

Parameter Passing

Packages

Basic Exception Handling

Strings

Reading Input

ArrayLists and Linked Lists

Arrays

Static Fields and Methods

Programming Style

 

Tag page
Pages that link here
Page statistics
652 view(s), 9 edit(s), and 6193 character(s)

Comments

You must login to post a comment.

Attach file

Attachments