A computer program is a sequence of instructions given to the computer in order to accomplish a specific task.
Programming is the preparation and writing of programs for computers.
Programmers write their programs in a high level programming language such as Java, C++, Python, Ruby or Scala.
A Java compiler instead of translating Java code to machine language code, translates it into Java Bytecode (a highly optimized set of instructions)
When the bytecode (also called a Java class file) is to be run on a computer, a Java interpreter, called the Java Virtual Machine (JVM), translates the bytecode into machine code and then executes it.
The advantage of such an approach is that once a programmer has compiled a Java program into bytecode, it can be run on any platform (say Windows, Linux, or Mac) as long as it has a JVM running on it.
To write a Java program, you will need a Text Editor (for writing the code) and a Java compiler (for compiling the code into bytecode).
There are a wide variety of Java Integrated Development Environments (IDEs) available in the market that come equipped with a text editor and a Java compiler thus simplifying writing, compiling and executing Java programs.
Most of them are freely downloadable from the Internet.
We will be using the open source and free Java Net Beans IDE for writing Java programs.
So let's dive straight in and write a simple Java program that prints a Welcome Message (“Hello World”) on the screen.
Since this will be your first Java program, you will first need to setup the programming environment.
An assertion is a mechanism for effectively identifying/detecting and correcting logical errors in a program.
A multithreaded program is one that can perform multiple tasks concurrently so that there is optimal utilization of the computer's resources.
The run() method is the entry point for every new thread that is instantiated from the class.
Exceptions of the Exception type SQLException can occur while connecting or fetching data from the database and these can be caught using a try catch block.
There are two ways to write an assertion assert expression; assert expression1 : expression2.
An assert statement states a condition that should be true at a particular point during the execution of the program.
To enable assertions at runtime, you can enable them from the command line by using the –ea option or in NetBeans, Right click on your project>Properties> Run>VMOptions and type–ea in the text box next to VM Options.
An assert statement can be used to assert that the value of a variable should be greater than or equal to a certain value.
The first statement in an assert expression evaluates expression and throws an AssertionError if expression is false.
The first method to create a thread is to create a class that extends the Thread class from the java.lang package and override the run() method.
The second statement in an assert expression evaluates expression1 and throws an AssertionError with expression2 as the error message if expression1 is false.
In Java, threads can be created in two ways: by extending the Thread class or by implementing the Runnable interface.
All software development in NetBeans is organized in the form of Projects, so we begin a new Project.
There are 5 columns in the book table and they can be accessed using a for loop and the getString() method of the ResultSet object.
In the IDE, click File> New Project (Ctrl + Shift + N) to create a new Java Application Project.
It is good practice to make variable names meaningful.
To assign a text value to a String variable we enclose the text between double quotes.
Variable names written in capital letters differ from variable names with the same spelling but written in small letters.
A variable of the primitive data type char can be used to store a single character.
The + operator is used for addition, the - operator for subtraction, the * operator for multiplication, the / operator for division, the % operator for modulus, and the ==, !=, >, <, >=, <= operators for comparison.
Java executes the instructions in sequential order, that is, one after the other.
You can define multiple variables of the same type in one statement by separating each with a comma.
In the Percentage Calculator program of the previous section, we used variables to store numeric data.
Quite often we want variables to store textual data, for example, the name of a student.
To store more than one character, we use the String class in Java.
A program is nothing but a sequence of instructions.
When used with Strings as operands, the + operator concatenates the strings together.
To assign a value to a char variable we enclose the character between single quotes.