Save as myfirstappjava 2 compile javacmyfirstappjava 3

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hod here) CPSC 324 ‐‐ Spring 2010 17 Writing a class with a main •  In Java, everything goes in a class –  Edit your source code file … with a .java extension –  Compile it into a new class file … with a .class extension –  When you run your program, you are running a class •  The JVM runs a program by loading the class and executing its main method –  Programs usually have multiple classes (source files) –  But the program starts from one class CPSC 324 ‐‐ Spring 2010 18 9 1/14/10 Writing a class with a main 1.  Save as:  MyFirstApp.java 2.  Compile:  javac MyFirstApp.java 3.  Run: java MyFirstApp  CPSC 324 ‐‐ Spring 2010 19 The Java on-line API documentation The Java API docs are very useful when writing code! –  http://java.sun.com/javase/6/docs/api/ –  or follow the link from the course webpage •  Contains all the classes in the standard libraries, their methods, and comments •  The documentation is built  using javadoc –  We’ll talk more about this  program later CPSC 324 ‐‐ Spring 2010 20 10 1/14/10 Basic Java Programming Constructs CPSC 324 ‐‐ Spring 2010 21 Java Data Types •  In Java, all variables must have a declared type •  Java distinguishes between “primitive” and “nonprimitive” types –  Primitive types are basic values (numbers, characters, etc.) –  Array and reference types are the non-primitives •  We’ll talk more about these later CPSC 324 ‐‐ Spring 2010 22 11 1/14/10 Java Data Types •  This distinction means Java is not a “pure” objectoriented language (since it has non-object values) –  Java does provide object “wrappers” for primitive types –  Since Java 1.5, automatic “autoboxing” and “unboxing” operations are supported (more on this later) •  Like C++, Java is case sensitive –  So, e.g., a variable “x” is different than “X” –  Or “main” is not the same as “Main” or “MaiN”, etc. CPSC 324 ‐‐ Spring 2010 23 Java Primitive Data Types Integer types –  ranges of Java integer types are not dependent on the architecture •  int –  Example: int i = 1999999; // declares var i, sets it to value –  4 bytes (-2,147,483,648 to 2,147,483,647) •  short –  short s = 19999; –  2 bytes (-32,768 to 32,768) CPSC 324 ‐‐ Spring 2010 24 12 1/14/10 Java Primitive Data Types •  long –  long l = 5999999999999999999L; // need the L here! –  8 bytes (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) •  byte –  byte b = 99; –  1 byte (-128 to 127) CPSC 324 ‐‐ Spring 2010 Java Primitive Data Types Integer types example 25 This prints: i = 1999999 s = 19999 l = 5999999999999999999 b = 99 public class IntegerTest { public staPc void main(String args) { int i = 1999999; short s = 19999; long l = 5999999999999999999L; byte b = 99; System.out.println("i = " + i + "\...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 324 at Gonzaga.

Ask a homework question - tutors are online