Java was designed to be a write once, run anywhere language. While this is mostly true, there are always a few caveats.
Because Java allows such flexibility, Android was built on top of it. Now you can write Android apps that run on TVs, phones, watches, etc. The limitations of some of these devices means that while in theory the applications would run, in reality, we may need to create some special versions.
However, to start, we’re going to just look at some plain old Java apps, to make sure we’re up to speed on our Java.
Luckily, Java is very similar to C++, JavaScript, and many other languages. So if you’ve used one of them, moving to Java should be simple. We’re going to assume that you have a Java Compiler (javac) and Java Runtime (java) already on your machine, and they are properly configured.
A couple notes about Java syntax:
- case sensitive
- strongly typed (i.e. int, string, etc are needed to define variables, return types, etc)
- object oriented (so classes are needed, for everything)
- semi-colons are required for an end of statement
- braces {} are needed to contain blocks of code
Let’s look at a most simple code:
public class Refresher01 {
/* This is a simple java program.
* This will print 'Hello World' as the output
*/
public static void main(String args[]) {
System.out.println("Hello World"); // prints Hello World
}
}
To compile it, we’d use javac JavaRefresher.java. That will create our byte code which the Java program will then be able to execute: java JavaRefresher.
Notice that the file name and the class name are the same.
Notice that the main function has to be both public and static and called main to run. Static means that the class doesn’t have to be instantiated to run, which it can’t be until a program is started. Public means that anyone (like us the user) can call the function. Main is just the keyword that the application knows to look for.
Common Syntax Styles and Rules
Java is case sensitive, with keywords being in lower case.
Camel Case – naming is generally camel case notation, where the first letter of each subsequent word is capitalized. The first letter of the name, is dependent upon what is is used for.
Class Names – should start with a capital letter, and the file should be the same name as the class.
Methods, functions in the Object Oriented world, and variables, should start with a lower case letter
Blocks should be indented, to make reading easier, but it is not required. Some people use tabs, others use four (4) spaces for each indent level.
Strings are enclosed in double quotes only. Single quotes are for characters.
Comments – There are two types of comments supported in Java. Sometimes call C and C++ comments, in Java, they are referred to as multi-line /* */ where everything between the two lines is ignored by the compiler, and single line // – where everything after the double forward slash is ignored, for the rest of that one single line.
Java Refresher (syntax, compiling) was originally found on Access 2 Learn