Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java basics

Every Java program (no matter how simple or complex) contains a fundamental structure. The simplest thing you can do in a Java program is print some text.

This tutorial focuses on:

Java program core code

Fundamental structure of a Java program consists of these elements:

A class declaration - A class is a grouping of related variables and functions (methods) that is used to achieve something. We explain variables and methods shortly, don't worry if right now you don't know what they are. All the source code for a Java program will be placed within the class definition. The class is given a name and the code within it is sorrounded by curly brackets.

Example:
class PrintText{ }

A main() method - A method is a grouping of code that executes when it is called. One method used in every Java program is the main() method, it's what makes a Java program work. The main() method has to be set with a few special keywords and a certain parameter like in the example below.

Example:
class PrintText{ public static void main(String[] args){ } }

The above example is a 'bare bones' Java program. It doesn't do anything, only contains the fundamental Java program structure.

Printing text

You can use two methods to print text in Java. Use double quotes for the text within each method.

System.out.print() method - prints a line of text
System.out.println() method - prints a line of text followed by a line break

Example:
class PrintText{ public static void main(String[] args){ System.out.println("Here is some text"); System.out.print("Here is some more text"); } }
Output:
Here is some text Here is some more text

NOTE: Every line of code in a Java program must end with a semicolon or an error will be generated!

Including comments in Java code

Comments are declared so that code would be easier to understand and to navigate. Comments can be placed anywhere within code. You can have single line comments or multi-line comments.

Single line comments - declared with two / symbols (span a single line)
multi-line comments - declared with a starting /* and an ending */ (span as many lines as you want)

Example:
/* This is a multi-line comment This program will print two lines of text */ class PrintText{ public static void main(String[] args){ //print a single line of text followed by a line break System.out.println("Here is some text"); //print another line of text //with no line break afterwards System.out.print("Here is some more text"); } }
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information