Getting Started

Description

In the video lesson, you created a Java class called 'MyLittleProgram' and created a method with the name 'main'.

In this lesson you will understand these terms in detail.

What is a class in Java?

A 'class' is the fundamental building block in Java programming language. Your program, which consists of Java code, starts by declaring a class and then adding your other coding constructs inside of it.

A simple valid class with the name Tutorial would be:

class Tutorial {

}

A class, in its simplest form is defined with class keyword and the name of the class. In this case, we called our class ‘Tutorial’ but you can call whatever you want as long as you follow identifier rules. Every class starts with a left curly brace {, and ends with a right curly brace }.

Even though Tutorial class is empty, and there is no other coding construct inside of it, it is still a valid class as you can compile this class without errors. You can create this class using any text editor. When you save the file on the filesystem, the file name name should exactly match the name of the class. This is especially true for classes with public modifier. You will learn more on modifiers in the next module. Although we did not add public keyword modifier for this class, it is still better to save the file as Tutorial.java to keep consistent coding practices.

To run a java program you have to follow two steps:

  • Compile your java program using javac executable.
  • Run your compiled program using java executable.

Let us understand both the steps in detail.

Compiling a Java Program

To compile this Java program file you would execute the below command from the Terminal:

javac Tutorial.java

javac is an executable which is part of JDK.

When you execute the above command a compiled file with the extension of .class is generated.

Note: Your filename should end with the .java extension. If you miss the .java extension the javac executable will not be able to compile your file to a .class file extension and instead you will see an error displayed with a message saying

error: Class names, 'Tutorial', are only accepted if annotation processing is explicitly requested.

If you see this message, rerun the command by saving the file with the .java extension.

Running a Java Program

You run the compiled version of your program by using the java executable. To run the compiled class you would execute:

java Tutorial

When the above command is invoked a JVM is spawned and the JVM will run your Java program.

Note: Notice when you run the java executable you will not use the .class extension. However, the java executable picks up the file name with the .class to execute the program.

But the above program will throw an exception when you try to run the program as there is no main method in the class. You will learn more about the main method below.

Java Setup

  • Open Google Cloud Shell using Link: http://console.cloud.google.com/
  • You need a Gmail a/c to get access to your own Google Cloud Shell
  • Find the java version installed on your computer by using java -version

In an IDE like AndroidStudio, as soon as you save your Java program file, IDE automatically compiles the file using javac and we do not have to do this step.

What is an IDE?

IDE stands for Integrated Development Environment. IDE is a complex software program consisting of numerous tools necessary to make software development easy for the Developer. With an IDE you never really compile and run using 'javac' or 'java' executables. 'javac' is automatically invoked as soon as your file is saved in the IDE and 'java' is invoked when you select a button typically called as 'run' in the IDE interface. Using an IDE improves productivity as it highlights errors in code even before it is saved, can easily search for methods, classes and dependencies and much much more. Popular IDE's for writing Java are Eclipse, IntelliJ, NetBeans etc..

For writing Android programs, the official IDE is AndroidStudio. Android programs can be written in Java and in this course we use Java to write Android Apps by using Android Studio. Those steps are explained in later chapters. However to understand the basic building blocks of Java programming language, we will use Cloud Shell which provides a simple platform to learn Java.

What is an Identifier?

The name of the class is called identifier literal in Java. Identifier literals are also used to name methods, variables etc., which you will learn later. All identifier literals are constructed with the same set of rules as below:

Rules for Identifiers

Cannot start with a number

Can have a mix of letters and numbers and special characters $ and _

Cannot use one of the reserved keywords

Cannot use spaces - E.g; 'my class' is illegal

Identifiers are case-sensitive. They must be written the same way every time you reference them in your program. e.g. MyClass is not the same as myClass

Reserved keywords

abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

Although it is hard to make a mistake of using the reserved keywords, if you are using an IDE, as most of the IDE's warns us as soon as you try to use it. And the reserved keywords are color coded in an IDE so it is hard to miss it. So it is actually trivial but it is still good to have a glance at the keywords. An IDE makes it easy, fast and less error prone to write Java programs when compared to using simple text editors.

Recommendation for class names

  • Computer does not care -- any name for the class, which satisfies the rules of the Identifier is fine. However you write programs to not only the computer to execute the program but also for other human beings to read and understand your programs! So it is recommended to give meaningful names for a class and also follow Java standard naming conventions: https://www.oracle.com/technetwork/java/codeconventions-135099.html
    A class name should start with an uppercase and should be of a noun form.

  • If you decide to give two words for your class name then use camel casing. E.g., MyLittleProgram. The first letter of every word starts with an uppercase letter.

  • Even though you can use $ and _ for constructing a class name, it is better to avoid using them. Some programs which generates java classes do use $ symbol in the automatically generated class names but human programmers tend to avoid them.

If you remember from the last lesson, you also constructed a main method and added a System.out.println statement inside the main method. So...

Basic Linux Commands That Every Developer Should Know

You used the terminal a.k.a console or command prompt to compile and run your Java program. Working from the terminal program is indeed a fundamental skill required in the software industry today. Do you know that you can copy, move or delete a file from the terminal instead of using the visual interface given by Google Cloud Shell? Although visual interface is more intuitive to work with, it is still essential to learn some of the basic linux commands using the terminal program if you intend to pursue a career in the software industry. Here is the video on some very fundamental linux commands that everyone in software industry should know.

results matching ""

    No results matching ""