WHAT IS JAVA?
Java is a high- level programming language. But what does it mean for a programming language to be high? First I should tell you about the lower levels. The "lowest" is the binary language. Which also known as machine language. It consists of 1's and 0's. When we go one level higher we see assembly language. Which is an English like language. Has only simple commands. When I say simple commands I don't mean "simple" language. For example, to display "Hi everyone"you have to write the code below:
section .text
global _start
_start:
mov edx,len
mov ecx,msg
mov ebx,1
mov eax,4
int 0x80
section .data
msg db 'Hi everyone' , 0xa
len equ $ - msg
These two languages (binary and assembly) together considered as low-level languages. One step higher of that is high-level languages. And Java comes right here. A language is high-level when it can be written in "English English" and it can be written machine independent.
Java runs on a variety of platforms such as Windows, Mac OS, and the various versions of UNIX. Which makes it highly portable (That's the reason you can play Minecraft on Mac :))
Java is Object Oriented. What that means is that everything is an Object. Like Class. And you will see it when we write our first hello world as a tradition. Also, one last-important thing about Java is that: Java is Platform Independent. What that means is that: when you compile Java code, it is not compiled into platform specific machine, but into platform-independent bytecode. This bytecode is interpreted by JVM (Java Virtual Machine) no matter whichever platform it is being run on.
JAVA EDITORS
You can write Java on Notepad if you would like. (Don't forget that extension for java files is .java).
You can also write in Netbeans or Eclipse. I will probably use Eclipse for examples.
SOME IMPORTANT THINGS BEFORE STARTING
Case Sensitivity--> Java is case sensitive. Which means is that two variables that named "number" and "Number" refers to different variables.
Class Names--> For now, it is not an important note but you will probably thank me at one point for this. All class names should start with an uppercase letter.
Method Names--> All method names should start with a lowercase letter.
Program File Name--> It should be same with the class name. Very important.
public static void main(String args[])--> This indicates your main method. Java processing starts from this method. It MUST exist in the Java program. Otherwise, it can't understand where to start.
JAVA VARIABLE TYPES:
There are 8 types of Primitive data types in Java. Which are:
boolean--> true or false. Its default value is false.
byte--> It takes an integer. Up to 8 bits. Its minimum value is -128 and the maximum value is 127. Its default value is 0.
char--> Represents 16-bit Unicode character. Its minimum value is '\u0000' or 0. Its maximum value is '\uffff' or 65,535. Its default value is '\u0000'.
int--> Stands for integer. Takes minimum value -2^31, maximum value 2^31-1 and default value 0.
double--> We use this variable type to write non-integer numbers. Its name is double because takes double-precision 64-bit IEEE 754 floating point. Its default value is 0.
short--> 16-bit signed two's complement integer. Minimum value: -32,768, Max value: 32,767 and default:0.
long--> Just like short but longer. It is 64-bit. That means a lot.
float--> Just like double. Shorter. 32-bit. You can use it to save memory. Especially in large arrays of floating point numbers.
HONORABLE MENTION --> String--> I know that it is not a primitive data type. But it is used a lot. Even in the main method (As you can see on "some important things before starting"). It takes an "ARRAY" of chars. (That's the reason the main method writes it as "String args[]" those brackets stands for an array as you can see again on "some important things before starting"). But what does it mean to take an array of chars? In English, it means that it takes words, sentences. Its default value is null.
FIRST PROGRAM IN JAVA
Let's start with displaying "Hello Universe".
class Hellooo
{
public static void main (String args[])
{
System.out.println("Hello Universe");
}
}
IMPORTANT THINGS ABOUT THAT CODE: First of all, I want to say that, the most important thing to be a good coder is, putting same brackets on the same vertical line. (It is not true but I think it looks waaay better.) The second important thing is that how I wrote the code. Class name starts uppercase, the main method exists, and my command is written as "System. out.println()". This indicates that my println function is (stands for "print line", after printing cursor goes to below line) taken from System class. This way of writing is only something that you can see on object-oriented languages. One more important thing is that we use ";" to indicate that our command is over. If you forget putting one you will have a hard time trying to find where the problem is. Because it won't compile.
COMMENT LINE:
To indicate comments we usually use "//". Comment lines are used for coders comments on the code. We recommend (even if we fail to use it properly) using comments often to explain what your code does in methods and complicated, hard to read commands. An example of the use of comments shown below:
class Hellooo{
public static void main (String args[])
{
System.out.println("Hello Universe"); //prints hello universe
}
}
JAVA IF-ELSE:
The syntax of if-else is stated below:
if (condition)
{
//code to be executed
}
The condition here is a code that returns true or false. For example:
class Hellooo
{
public static void main (String args[])
{
if (1>2)
{
System.out.println("Hello Universe");
}
}
}
This code won't display "Hello Universe" because 1>2 = false. But below code will:
class Hellooo
{
public static void main (String args[])
{
if (1<2)
{
System.out.println("Hello Universe");
}
}
}
Else is used with if statements. What it means is that do if block if condition is true else(if condition is not true) do else block. An example code is shown below:
class Hellooo
{
public static void main (String args[])
{ int a=2;
if (a>1)
{
System.out.println("A is greater than 1");
}
else
{
System.out.println("A is not that great.");
}
}
}
This code above will print "A is greater than 1" to console (without "s). Because we created an int variable and told that its value is 2. Then our code looked into if statement's condition and saw that it is true and compiled it. There was no reason to look else block.
If there was more than one if what would happen? Let's look at the code below:
class Hellooo
{
public static void main (String args[])
{ int a=2;
if (a>0)
{
System.out.println("A is greater than 0");
}
if (a>1)
{
System.out.println("A is greater than 1");
}
else
{
System.out.println("A is not that great.");
}
}
}
This code would print both "A is greater than 0" and "A is greater than 1". Because it looked at the if statement and condition provided. Compiled first if, then saw second if. Condition provided again and block compiled. There was no reason to compile else block, because if conditions are provided. To compile else block, the code should have passed all if statements and had no chance but to compile else block.
There is also else if statement. Let's look at an example:
class Hellooo
{
public static void main (String args[])
{ int a=2;
if (a>0)
{
System.out.println("A is greater than 0");
}
else if (a>1)
{
System.out.println("A is greater than 1");
}
else
{
System.out.println("A is not that great.");
}
}
}
What I did is to simply change second if statement to an else if statement. But now console will only display "A is greater than 0". Because what else if does is, it only compiles when if statements ignored but its condition is provided.