DEV Community

iamdestinos
iamdestinos

Posted on

Java Basics: Getting Started (File Syntax and Variables)

Intro

Java is an object oriented language owned by Oracle and operates on the premise of "write once, run anywhere". This is accomplished by the fact Java is comprised not only of a programming language, but also a virtual engine that runs the code. While this makes running Java somewhat easy once a user knows what to do, learning how to use Java from scratch can seem rather daunting. This article is here to help get through the beginning of using Java as a programming language.

Installing Java

The first step to using Java is to install it. Java can be installed here and instructions for setting it up for windows can be found on w3schools.

Basics of Using Java

Syntax

The first thing to know when using Java is syntax, as Java has rather strict requirements for how to set up files.
For Java, the first file in a Java application is the Main.java, which contains this:

public class Main {
  public static void main(String[] args) {
    // whatever contents the user puts here
  }
}
Enter fullscreen mode Exit fullscreen mode

The first line in the example is the class, which is required for every java file. Classes have a few requirements, mainly that the name must start with a capital letter and the class must share its name with the file (so Main is the class for Main.java, but if the class was called Example, the file would need to be named Example.java).

The second line contains the main method, which is distinctly different from the class. Unlike the class name, which can be different depending on the file name, every file must have a method with the name main. The main method's purpose is to contain all code that will be executed in the file.

Basic Variables and Statements

Now that the basics of Java file structure has been discussed it is time to talk about the basic variables Java uses and some helpful statements.

Comments and Console Logs

//This is a single line comment
/* This is
a multiline comment */
System.out.println("Hello World"); //will print content to current line in console and anything printed after starts on a new line
System.out.print("Hello World"); //will print on current line and next print will continue on same line in console
Enter fullscreen mode Exit fullscreen mode

Variable declaration

To declare a variable in Java, users must first declare the type of the variable followed by the name and assigned value, as shown below:

String exampleStr = "Hello there"; //variable declaration of type string
int integerVal = 50; //int variables store non decimal numbers
float floatingPointVal = 50.55; //floating point variables store numbers with a decimal
char character = "D"; //character variables store a single character
boolean bool = true; //boolean variables store boolean values (true/false)
Enter fullscreen mode Exit fullscreen mode

Closing

To code with Java users have to make sure every file contains a class sharing the file name which contains a main method within it. Users must also make sure all variables are declared with their given type as well as a name (also called an identifier).

Sources

w3schools Java tutorial
Oracle website

Top comments (0)