| CS 535: Object-Oriented Programming & Design |
|---|
| Reference | |
| Basics | slide # 1 |
| ...First Program | slide # 3 |
| ...Syntax | slide # 9 |
| ...IO | slide # 14 |
| ...Basic Data Types | slide # 17 |
| ......Primitive Type Ranges | slide # 18 |
| ......NaN, +infinity, -infinity | slide # 23 |
| ......Ints and Booleans are Different! | slide # 26 |
| ......Characters | slide # 28 |
The Java Language Specification, Gosling, Joy, Steele
Uses garbage collection
Uses standard C/C++ control structures
Java is strongly typed
sdsu.java
Web Sites (few among hundreds)
Yahoo
class HelloWorldExample
{
public static void main( String args[] )
{
System.out.println("Hello World");
}
}
setenv CLASSPATH '.:/opt/local/lib/java/classes.zip'
rohan 34-> ls
FirstProgram.java
rohan 35-> javac FirstProgram.java
rohan 36-> ls
FirstProgram.java HelloWorldExample.class
rohan 37-> java HelloWorldExample
Hello World
class HelloWorldExample
{
public static void main( String args[] )
{
System.out.println("Hello World");
}
}rohan 42-> java -cs HelloWorldExample
The -cs (or -checksource) option recompiles all needed files and runs the program
A file is recompiled if it has not been compiled yet
or the binary file is older than the source file
For this example the directory will be
setenv CLASSPATH '.:/opt/local/lib/java/classes.zip:/home/ma/whitney/java/classes'
Note the above must not contain any newlines
Place the file HelloWorldExample.java in or in any subdirectory of "~whitney/java/classes"
From any location you can compile and run the program using:
rohan 42-> java -cs HelloWorldExample
Incremental compiling while developing:
// C++ comment works
/** Special comment for documentation
class Syntax
{
public static void main( String args[] )
{
int aVariable = 5;
double aFloat = 5.8;
if ( aVariable < aFloat )
System.out.println( "True" ) ;
int b = 10;
char c;
c =
'a';
}
}class Syntax {
public static void main( String args[] ) {
int aVariable = 5;
if ( aVariable < aFloat )
System.out.println( "True" ) ;
}
}
class Syntax
{
public static void main( String args[] )
{
int aVariable = 5;
if ( aVariable < aFloat )
System.out.println( "True" ) ;
}
}
class Syntax
{
public static void main( String args[] )
{
int aVariable = 5;
if ( aVariable < aFloat )
System.out.println( "True" ) ;
}
}
Names of classes
Names of variables and functions
Names of constants
All Java programs should use these conventions
What to do if your team has adopted "bad" style?
class Output
{
public static void main( String args[] )
{
// Standard out
System.out.print( "Prints, but no linefeed " );
System.out.println( "Prints,linefeed at end" );
double test = 4.6;
System.out.println( test ); // prints 4.6
System.out.println( "You can use " +
"the plus operator on " +
test +
" String mixed with numbers" );
System.out.println( 5 + "\t" + 7 ); // prints 5 7
System.out.println( "trouble" + 5 + 7 ); // trouble57
System.out.println( "OK" + (5 + 7) ); // OK12
System.out.flush(); // flushes output buffer
System.err.println( "Standard error output" );
}
}import sdsu.io.Console;
public class Test_SDSU_Console
{
public static void main( String[] args )
{
Console.println( 5 );
Console.print( "Hi Mom" );
Console.println( " Hi Dad" );
Console.print( "Print an integer ");
int why = Console.readInt();
Console.println( "You typed: " + why );
int theEasyWay =
Console.readInt( "Print another integer" );
Console.print( "You typed: %20i\n ", easy );
String message =
Console.readLine( "Type a line of text" );
Console.println( "");
double x = 1.23456789012;
Console.print( "x = %f\n", x);
Console.print( "x = % .5f\n", x);
Console.print( "x = %10.8f\n", x);
}
}
SDSU Java Class library documentation
Use of the on-line Java docs can not be avoided
Learn to use it now
class PrimitiveTypes
{
public static void main( String args[] )
{
// Integral Types
byte aByteVariable; // 8-bits
short aShortVariable; // 16-bits
int aIntVariable; // 32-bits
long aLongVariable; // 64-bits
// Floating-Point Types
float aFloatVariable; // 32-bit IEEE 754 float
double aDoubleVariable; // 64-bit IEEE 754 float
char aCharVariable; // always 16-bit Unicode
boolean aBooleanVariable; // true or false
}
}| type | from | to |
| byte | -128 | 127 |
| short | -32,768 | 32,767 |
| int | -2,147,483,648 | 2,147,483,647 |
| long | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
| Equality | = != |
| Relational | < <= > >= |
| Unary | + - |
| Arithmetic | + - * / % |
| Pre, postfix increment/decrement | ++ -- |
| Shift | << >> >>> |
| Unary Bitwise logical negation | ~ |
| Binary Bitwise logical operators | & | ^ |
class Operations
{
public static void main( String args[] )
{
int a = 2;
int b = +4;
int c = a + b;
if ( b > a )
System.out.println("b is larger");
else
System.out.println("a is larger");
System.out.println( a << 1); // Shift left: 4
System.out.println( a >> 1); // Shift right: 1
System.out.println( ~a ); // bitwise negation: -3
System.out.println( a | b); // bitwise OR: 6
System.out.println( a ^ b); // bitwise XOR: 6
System.out.println( a & b); // bitwise AND: 0
}
}
| Equality | = != |
| Relational | < <= > >= |
| Unary | + - |
| Arithmetic | + - * / % |
| Pre, postfix increment/decrement | ++ -- |
Underflow results in zero.
An operation that has no mathematically definite result produces NaN - Not a Number
NaN is not equal to any number, including another NaN
class NaN
{
public static void main( String args[] )
{
float size = 0;
float average = 10 / size;
float infinity = 1.40e38f * 1.40e38f;
System.out.println( average ); // Prints Inf
System.out.println( infinity ); // Prints Inf
System.out.println( infinity * 0 ); // Prints NaN
}
}
class Compare
{
public static void main( String args[] )
{
float nan = Float.NaN;
float positiveInfinity = Float.POSITIVE_INFINITY;
float negativeInfinity = Float.NEGATIVE_INFINITY;
// The following statements print false
System.out.println( nan == nan );
System.out.println( nan < nan );
System.out.println( nan > nan );
System.out.println( positiveInfinity < positiveInfinity
);
// The following statements print true
System.out.println( positiveInfinity == positiveInfinity );
System.out.println( 5.2 < positiveInfinity );
System.out.println( 5.2 > negativeInfinity );
}
}class Casting
{
public static void main( String args[] )
{
int anInt = 5;
float aFloat = 5.8f;
aFloat = anInt; // Implicit casts up are ok
anInt = aFloat ; // Compile error,
// must explicitly cast down
anInt = (int) aFloat ;
float error = 5.8; // Compile error, 5.8 is double
float works = ( float) 5.8;
char c = (char) aFloat;
double aDouble = 12D;
double bDouble = anInt + aDouble;
int noWay = 5 / 0; // Compile error,
int zero = 0;
int trouble = 5 / zero; // Metroworks Compiler error
// No error on Sun compiler
int notZeroYet;
notZeroYet = 0;
trouble = 5 / notZeroYet ; // No compile error!
}
}
class UseBoolean
{
public static void main( String args[] )
{
if ( ( 5 > 4 ) == true )
System.out.println( "Java's explicit compare " );
if ( 5 > 4 )
System.out.println( "Java's implicit compare " );
if ( ( 5 > 4 ) != 0 ) // Compile error
System.out.println( "C way does not work" );
boolean cantCastFromIntToBoolean = (boolean) 0;
// compile error
int x = 10;
int y = 5;
if ( x = y ) // Compile error
System.out.println( "This does not work in Java " );
}
}
Compiler usually complains about using variables
before explicitly giving them a value
class InitializeBeforeUsing
{
public static void main( String args[] )
{
int noExplicitValue;
System.out.println( noExplicitValue ); // Compile error
int someValue;
boolean tautology = true;
if ( tautology )
{
someValue = 5;
}
System.out.println( someValue ); // Compile error
}
}class CharactersLiterals
{
public static void main( String args[] )
{
char backspace = '\b';
char tab = '\t';
char linefeed = '\n';
char formFeed = '\f';
char carriageReturn = '\r';
char doubleQuote = '\"';
char singleQuote = '\'';
char aCharacter = 'b';
char unicodeFormat = '\u0062'; // 'b'
}
}