Java for Beginners

These notes are designed for those new to Java programming but assumes some programming experience.

Java SDK Installation

The Java System Develpoment Kit (SDK) is used to develop applications in Java. Download the Java (SDK) from Java SE Downloads. There are many versions and combinations available there. I recommend downloading the lates version with Net Beans. The Net Beans version will run immediately after installation, but if you download the SDK alone you'll have to make some system changes before it will work.

Java Tutorials

Java Tutorials lists Sun's Java tutorials. These are excellent and I recommend beginning with them, starting at the top of the list and working your way through them until you feel confident enough to start skipping around and looking for the instructions you need. "Hello World" has instructions for creating your first program in various environemnts and includes links for downloading the Java SDK.

NetBeans Documentation contains a list of tutorials for using NetBeans, a tool for developing Java applications. Begin with the Quick Start Guide.

Java Tips for Beginners

  1. Variable names can be any legal identifier (that is not a reserved word) beginning with letter, $ or _ and are case sensitive. Convention says begin variable names with lower case, make subsequent words in name begin with upper case and do not use _. e.g. countItemsInStock. Convention for naming constants is to make them all uppser case with _ between words. e.g. MAX_CHILDREN=255.
  2. Use 'single quotes' for char literals (e.g. '\u00F' for ) and "double quotes" for String literals (e.g. "This is an example string".
  3. Arrays contain a fixed number of values of a single type. The number of elements is defined when the array is defined and cannot be changed.
    anArray = new int[10]; // create an array of integers. elements are indexed 0 to 9
    anArray[0] = 100; // initialize first element
    int[] arrayIntegers = {1, 4, 3, 4, 7}; // creates and initializes and array of 5 elements
    String[][] names = {{"Alan", "Mary", "Alex"}, {"Smith", "Jones"}}; // creates and initializes a 2 dimensional array
  4. Operands
    • "All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left."
    • Unary operators are used to increment/decrement or assign negative value to one operand. result++ is temporary and does not change the value of result while ++result changes the value of result by adding 1.
    • Use == to test if 2 variables are equal. = is used only for assignment.
    • Use && for Conditional-AND and || for Conditional-OR.
    • Simple if .. then .. else is written as
      result = someCondition ? value1 : value2; // if somecondition is true, result=value1, else result=value2
    • instanceof returns true or false when determining if an object is an instance of a class or sublclass. Null is not an instance of anything.
  5. End statements with a ";".
  6. Control structures
    • If () {} else if () {} else {}
    • switch () { case value: statements; break; case value: statements; break;}
    • while () {} // statement not performed if criteria not met
    • do {} while (); // statement performed at least once
    • for (initialization; termination; increment) {}
    • break
    • continue
    • return
  7. Classes
    • By convention, class names are capitalized
    • By convention, method names are begin with lower case verb. Other words in name have first letter capitalized, no _
    • Methods must be defined as "void" (no value returned) or with the type of the return value. The later must have a return statement in the method, defining the value.
© Copyright 2023 Dotty Storer