1 / 10

Basic Java Programming

Basic Java Programming. CSCI 392 Week Two. Stuff that is the same as C++. for loops and while loops for (int i=0; i<10; i++) if else if (count != 10) increment, decrement, etc result *= factorial--; switch statements switch (answer) { case 'y': case 'Y': some stuff

Download Presentation

Basic Java Programming

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. Basic Java Programming CSCI 392 Week Two

  2. Stuff that is the same as C++ • for loops and while loops for (int i=0; i<10; i++) • if else if (count != 10) • increment, decrement, etc result *= factorial--; • switch statements switch (answer) { case 'y': case 'Y': some stuff break; default: }

  3. Java Statements Not in C++ • Labeling blocks of code outerloop: for (int i=0;…) for (int j=0; … if (something_bad) break outerloop; • Exception Handling try { value = x/y; } catch (ArithmeticException exceptvar) { System.out.println("Math Error"); }

  4. Boolean boolean - 1 bit Integers byte - 8 bits short - 16 bits int - 32 bits long - 64 bits Characters char - 16 bits Floats float double Strings String still be careful when comparing Basic Data Types

  5. Boolean - So what? This C++ feature is not legal in Java: int factorial, result = 1; . . . while (factorial) result *= factorial--; But neither is this common C++ error: if (count = 10) // not legal Java

  6. Variable Declaration Oddities • You must initialize a variable before it can be used, so most declarations include an initialization. • You can set a "constant's" value at run time. final int count = getCount(); • Arrays require both a definition and declaration. • int[] myarray; myarray = new int[25]; • int[] myarray = new int[25]; • char[] name = {'B', 'o', 'b'};

  7. Type Checking • You can assign small sized variables into bigger variables, but big variables don't fit into small variables. int X = 99; long Y = 0; Y = X; // legal X = Y; // not legal X = (int) Y; // legal

  8. Comments • Just like C /* multi-line */ // single line • Except… /** document comment **/ • not thrown away by compiler

  9. Simple Output • Printing to "Standard Output" System.out.print ("Hello " + name ); System.out.println (); • Formatted Output NumberFormat layout = NumberFormat.getNumberInstance(); layout.setMaximumFractionDigits(4); double x = 12.3, y = 45.6; String outstr = layout.format(x/y); System.out.println("x/y = " + outstr);

  10. Input - not so simple import java.io.* ; public class array_test_1 { public static void main(String[] args) throws Exception { InputStreamReader stdio = new InputStreamReader(System.in); BufferedReader instream = new BufferedReader (stdio); int count; System.out.print ("Enter an integer: "); String indata = instream.readLine(); count = Integer.parseInt (indata); for (int i=0; i<count; i++) System.out.print (i + " "); System.out.println(); } } improvement: catch the NumberFormatException

More Related