bembry.org
Home / Technology / Java

Java Lesson 1: Basic Concepts in Java

  • Concepts of Java
    • Java is a byte-compiled language. You compile source code into byte code, which is then interpreted by the Java Virtual Machine.
    • In theory, a Java application can be ported to any platform that has a Java Virtual Machine.
    • Java is built on classes and objects. You cannot have a java program without a class.
    • Source files have the extension .java , and object code (compiled code) has the extension .class .
    • Java is case sensitive. CapitalIZatIon CoUnTs!
    • When first learning Java, there are some things you will have to just type in faith. The language requires some complexities that beginning programmers will not understand for a while.
  • Building a Java Program
    1. Begin by defining the class.
      • Identify the class as "public", "private" or "protected".
      • Use the keyword "class".
      • Give the class a name. Note that the class name must be the same as the file name.
      • Use curly braces {} to show beginning and end of the class definition.
    2. Within the braces, define at least one method
      • Indentify the method as "public", "private" or "protected"
      • Use the keyword "static".
      • Identify the return type. Use "void" if no value is passed from this method to another part of the program.
      • Name your method. Use the name "main" if there is only one method.
      • In parentheses list the parameters used by the method.
      • Enclose the code for the method in more curly braces {}.
    3. Within the method braces, write code to do something
      • End each line of code with a semi-colon ;
      • Remember, capitalization counts
    4. Save the file.
      • The file name should be the same as the class name.
      • Use the extension .java when saving code.
    5. Compile the code
      • Using the JDK compiler, type javac filename.java
      • The compiled code will be named filename.class
      • If the compiler displays error messages, edit the code as directed and try to compile again.
    6. Run the code by typing java filename
  • Example
         public class HelloWorld 
         {
            public static void main(String[] args)
            {
                System.out.println("Hello everybody.");
            }
          }
         
    • Save the above code as "HelloWorld.java"
    • At a terminal, type javac HelloWorld.java to compile.
    • At a terminal, type java HelloWorld to run the program.
Restricted access