Java Basics: Variables, Syntax and Conventions
Java is a high-level programming language. This guide describes the basics of Java, providing an overview of syntax, variables, data types and operators.
This Webopedia Study Guide is an introduction to the Java programming language for the beginner student. No programming experience is required to use this guide. The basic Java topics covered in this guide provide the background knowledge required to understand more complex topics later, such as Object Oriented Programming.
Java is a high-level programming language that was developed by Sun Microsystems and initially released in 1995. The Java Virtual Machine (JVM) can run on essentially any platform, one big reason as why it's so widely used. Java is also:
- High Performance: Java uses Just-In-Time compilers to improve performance.
- Multithreaded: Java programs can perform multiple tasks at a time with the use of multithreading.
- Object Oriented: Java uses objects and classes to perform complex functions, or to model the real world, a very powerful feature.
Java Basics Study Guide Checklist
Java Basics Study Guide, Part I Getting Started: Key Terms to Know
Essential Tools
Java Variables: An Introduction
- Primitive data types
- Declaring a Variable in Java
Java Syntax and Basic Conventions
Java Programming Best Practices
Java Basics Study Guide, Part II
- Java Arithmetic Operators
- Java Relational and Logical Operators
- Java Modifiers
- Java Control Structures
- Summing it up
Getting Started: Key Terms to Know
The following tech definitions will help you to better understand basic Java programming:
- class
- function
- data structure
- Java
- JVM - Java Virtual Machine
- multithread
- object-oriented programming (OOP)
- procedure
- programmer
- syntax
Essential Tools
- The latest version of Java JDK
- Set up the environment variable path
- A command prompt
- A text editor (Notepad++ is a great free text editor that recognizes Java syntax and keywords)
- Your learning cap
Java Variables: An Introduction
When you go to work or school, you might take a lunch box. In this lunch box can be any type of object that is related to lunch — such as a sandwich, a cookie or a juice box. Now, you probably wouldn't keep pens and pencils in the lunch box. For that, you'd use a pencil case. Variables work much in the same way. In Java, variables can be of different data types, and hold information.
Java uses 8 different primitive data types
Java uses the following 8 primitive data types for its variables:
- int: a 32-bit integer
- short: a 16-bit integer
- long: a 64-bit integer
- byte: an 8-bit integer
- float: a 32-bit floating-point (decimal number)
- double: a 64-bit floating-point (decimal number)
- char: a 16-bit character (uses Unicode)
- boolean: a true or false value
Java also uses Strings, which are classes (not primitive data types) to store text. Strings are a bit unique in that if you don't use methods to access its class functionality, it behaves exactly like a primitive data type with a slightly different declaration.
Declaring a Variable in Java
To use a variable in Java you must declare it. The declaration of a variable creates a space in memory to store the information that you will assign to the variable. Here are some examples:
Examples of variable declaration in Java
The datatype is lowercase (except for the String, which is the exception because it is a class). Typically, the name of the variable uses camel case, meaning the first letter is lowercase and every other word in the variable name is capitalized.
For example: thisIsASentenceInCamelCase
Also, make sure to name your variables sensibly. If a variable is holding the name of a school, call it schoolName. Your code should be easily understood by other programmers who read it. Once a variable is declared, you can change the value at any time without repeating the datatype. For example:
Java Syntax and Basic Conventions
Java, much like English, has grammar rules you must abide by called a syntax. If you don't follow the Java syntax, your programs will not compile. Here are the basics you need to know:
- Every line of code ends with a semicolon;
- Java is case sensitive, therefore, two variables called myfirstvariable and MYFIRSTVARIABLE are two separate references in memory
- (Parentheses are often used) because (much like in mathematics), they help ensure that operations are done in the correct order
- ((parentheses don't make (sense if they are not closed properly and
- {Braces are a used to divide code up into blocks. You can write 40000 lines of code inside the braces of an if-statement, and all 40000 lines will belong to that if-statement}
- [Brackets are used when creating arrays]
- Blank lines don't mean anything to Java, but can definitely make your code more readable by breaking it up into chunks
- //Double forward slash indicates that this line is a comment, and will be
- //Ignored by the compiler. It is simply used to leave notes on your code
- //To make it more understandable.
- /** Additionally, you can use this syntax
- To start a multi-line comment
- That ends here **/
- When doing arithmetic, you must use the proper data types. You can add the integers 1 and 2, but you can’t subtract true from false, or "Sentence" from "Hello"
Additionally, every program you run must have a 'main' method, or else it will not run.
Here is an example:
Example of a basic Java Program (Hello World!)
This program simply prints "Hello World!" to your console. This is a famous function that is often used for a beginner programmer's first program. Try it out in your text editor or IDE.
All your programs will have this basic structure — a single class and a main method. This is the proper syntax and you include it every time. It's a good idea to memorize this basic set up. Once you have developed your programming skills and start to use object-oriented methods, your programs will take on much different forms than the one shown above. Beginners, however, should follow this convention:
Example of basic Java convention
Save your program as NameOfYourProgram.java, as the filename must match the class name.
Java Programming Best Practices
In programming, there are ways to write code that are technically correct and ways to write code that are conventional and easy to understand. It's often said that the hardest part of programming is naming things. In Java, naming conventions are typically as follows:
- Variables are named with camel case as shown in the variable section.
- Methods are named with camel case.
- Classes and filenames are capitalized like this: ThisIsTheNameOfMyClass.
- Variable names should always be descriptive to improve readability. For example if you are storing the average weekly temperature in a variable, call it something along the lines of averageWeeklyTemperature as opposed to variable5 which is not descriptive at all.
- Much like variables, method names should describe their function. If a method calculates the average weekly temperature, you might want to call it calcWeeklyTemp instead of method4.
Aside from naming conventions, another important practice is to comment code to explain what the variables are used for, what methods do and so on.
QUICK TIP: If you are writing programs as assignments or tests, remember that using good naming conventions and comments will allow your teacher to understand your code. Later, if you sell a program and it is modified by another programmer they may not know what it is supposed to do. Always use correct naming conventions and comments.
When using control structures and methods, use indentation to show which lines of code belong to which set of braces. Here is an example:
Java Basics: Example Indentation
>> NEXT PAGE Java Basics Study Guide, Part 2
WEBOPEDIA NEWS
Stay up to date on the latest developments in Internet terminology with a free newsletter from Webopedia. Join to subscribe now.