|   | CS 596 Java Programming Fall Semester, 1998 Basic Java Syntax | |
|---|---|---|
| © 1998, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 31-Aug-98 | 
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;            // This is legal in Java
      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" ) ;
      }
   }
| abstract | default | implements | protected | throws | 
| boolean | do | import | public | transient | 
| break | double | instanceof | return | try | 
| byte | else | int | short | void | 
| case | extends | interface | static | volatile | 
| catch | final | long | super | while | 
| char | finally | native | switch |  | 
| class | float | new | synchronized |  | 
| const | for | package | this |  | 
| continue | if | private | throw |  | 
| byvalue | cast | future | generic | goto | 
| inner | operator | outer | rest | var | 
class Output 
   {
   public static void main( String args[] ) 
      {
         // Standard out
      System.out.print(  "Prints, but no newline" );
      System.out.println(  "Prints, adds platforms newline at end" );
      double  test  =  4.6;
      System.out.println(  test  );
      System.out.println( "You can use " + "the plus operator on " 
                     + test  + "  String mixed with numbers" );
      System.out.println(  5  +  "\t"  +  7  );
      System.out.println(  "trouble"  +  5  +  7  );   //Problem here
      System.out.println(  "ok"  +  (5  +  7)  );
      System.out.flush();      // flushes output buffer
      System.err.println(  "Standard error output"  );
      }
   }
Prints, but no linefeed Prints, adds linefeed at end 4.6 You can use the plus operator on 4.6 String mixed with numbers 5 7 trouble57 ok12 Standard error output
import sdsu.io.Console;
public class Test_SDSU_Console
   {
   public static void main( String[] args )
      {      
      // Simple printing
      Console.println( 5 );
      Console.print( "Hi Mom" );
      Console.println( " Hi Dad" );
      
      Console.print( "Print an integer ");
      int why = Console.readInt();
      Console.flushLine();            // flushes newline from input
      Console.println( "You typed: " + why );
      
      int easy = Console.readInt( "Print another integer" );
      Console.flushLine();
      Console.print( "You typed: %20i\n ", easy );
      String message = Console.readLine( "Type a line of text" );
      Console.flushLine();
      Console.println( ":" + message + ":");
      
      }
   }
5 Hi Mom Hi Dad Print an integer 12 You typed: 12 Print another integer 34 You typed: 34 Type a line of text Hi Mom :Hi Mom:
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
      // Character Type
      char   aCharVariable;      // always 16-bit Unicode
      // Boolean Types
      boolean   aBooleanVariable;      // true or false
      }
   }
| type | Min | Max | 
| 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 | 
class ArithmeticLiterals 
   {
   public static void main( String args[] ) 
      {
      long   aLong   =  5L;
      long   anotherLong  =  12l;
      int    aHex  =  0x1;
      int    alsoHex  =  0X1aF;
      int    anOctal  =  01;
      int    anotherOctal  =  0731;
      long    aLongOctal  =  012L;
      long    aLongHex  =  0xAL;
      float    aFloat  =  5.40F;
      float    alsoAFloat  =  5.40f;
      float    anotherFloat  =  5.40e2f;
      float    yetAnotherFloat  =  5.40e+12f;
      float    compileError = 5.40;         //Need cast here
      double    aDouble  =  5.40;         //Double is default!!
      double    alsoADouble  =  5.40d;
      double    moreDouble  =  5.40D;
      double    anotherDouble  =  5.40e2;
      double    yetAnotherDouble  =  5.40e+12d;
      }
   }
| 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
 | ++
   --
 | 
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 Infinity
      System.out.println(  infinity  );      // Prints Infinity
      }
   }
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; // anInt  is cast up to double, 
      int  noWay  =  5 /  0;      // Compile error, compiler detects
                           // zero divide
      int  zero  =  0;
      int  trouble  =  5 / zero;   //Some compilers may give error here
      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 " );
      }
   }
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'
      }
   }