dcsimg
The Wayback Machine - https://web.archive.org/web/20190505164142/https://www.webopedia.com/guides/java-basics.html
Main » guides »

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.

Webopedia Study GuideThis 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
Webopedia Study Guide Section Getting Started: Key Terms to Know
Webopedia Study Guide Section Essential Tools
Webopedia Study Guide Section Java Variables: An Introduction
   Webopedia Study Guide Section - Primitive data types
   Webopedia Study Guide Section - Declaring a Variable in Java
Webopedia Study Guide Section Java Syntax and Basic Conventions
Webopedia Study Guide Section 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:

Essential Tools

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:

Java Declare Variable
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 Int Sum

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:

Java Basics Hello WorldExample 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:

Java basics conventionExample 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 Indentation
Java Basics: Example Indentation

>>  NEXT PAGE Java Basics Study Guide, Part 2










LATEST ARTICLES
Facts about IT & Coding Boot Camps

The following coding and IT boot camp facts and statistics provide an introduction to the changing trends in education and training programs. Read More »

Top Cloud Computing Facts

The following facts and statistics capture the changing landscape of cloud computing and how service providers and customers are keeping up with... Read More »

STUDY GUIDES
Java Basics, Part 1

Java is a high-level programming language. This guide describes the basics of Java, providing an overview of syntax, variables, data types and... Read More »

Java Basics, Part 2

This second Study Guide describes the basics of Java, providing an overview of operators, modifiers and control Structures. Read More »

Network Fundamentals Study Guide

Networking fundamentals teaches the building blocks of modern network design. Learn different types of networks, concepts, architecture and... Read More »