WRITING SIMPLE JAVA PROGRAM RCS 102 (22/3/16)

https://rucuso.blogspot.com/2016/03/writing-simple-java-program-rcs-102.html
As typed By Okiki
Things to consider
- All java code are written inside a class
- A java program must consist of a class which is literally a blue print / template of a program
- In writing a java program we start by the key word "public" which means that the it is accessible anywhere in the code, then "class" which defines it as the basic unit, then follows a class name e.g HelloWorld and opens a curly bracket which initiates the start of the block of code thereafter close the curly bracket.
- Example
public class HelloWord{
//block of code here
}
- The name of the class by convention starts with a capital letter
- Not all java programs are programs even if every java program must have at least one class.
- A class that contains the main method is the main class.
- Main Method is
public static void main(String[] args({
//code here
}
- Curly brackets signifies the beginning and ending of a block of code. Static method does not depend on the creation of objects, it just executes as it is not part of an object it is just a part of the class. Void means it is not a value return method. Normal brackets are conventionally used in methods to indicate that the object is a method. Square brackets indicate that an object is an Array.
- To output anything to the screen, we have to use a special class system
- example
System.out.println("Hello World");
- Each statement must end with a semi-colon " ; " this tells the compiler that the statement has come to an end.
Variables
- These are named memory locations. Variables comprises of the following;
- Name of the variable
- Content of the variable
- Type of the content of the variable
Data types in Java
- They are divided into two major types
- Primitive Data types
- It is a data type that is indivisible, meaning it can not be divided into smaller parts. It is said mythically to be atomic in nature
- Numeric Data types
- Whole numbers
- Byte
- Int
- Long
- Short
- Floating Point numbers
- Float
- Double
- Non-Numeric data types
- Char - it is of 2-bytes as it is a Unicode character
- Boolean - Occupies 1 bit i.e 0 or 1
- Reference data types / Objects
- These are types which are made from classes
- String are reference
Variable declaration syntax
- Syntax - The rules of a certain language. They are grammatical rules of a programming language
[variable-type variable-name] e.g int age;
- Initalize : Giving an initial value to a variable.
- In some cases you can declare and initialize at the same time.
e.g. double distance = 10.1;
- Data types are associated with operators. These iinclude;
- Arithmetic Operators
- + - Addition
- - Subtraction / Minus
- / Division
- * Multiplication
- % Modulus division
- Assignment Operators
- =
- x ++ Post Increment
- x -- Post-Decrement
- ++x Pre-Increment
- --x Pre-Decrement
- Relational Operators
- > Greater than
- < Less than
- == Exactly equal to
- ! Not
- >= Greater or Equal to
- <= Less or Equal to
- || OR
- && And
ReplyDeleteHi Admin, its an informative post, Java is most popular programming language and it offer huge career prospects for the talented professionals.
J2EE Training in Chennai|JAVA Training Chennai|JAVA J2EE Training in Chennai