Java programming for the beginners
Hello. I'm here to give you lesons on Java and will post an article appr. every 2-3 days. Those who used to program in C/C++ will see many similarities between C++ and Java. it is so becasue java is a language that combines a feature of C++ and SmallTalk OOL(object oriented language) so i will start with the basic stuff.
Your first few programs will have the general form:
// Your Name
// Computer Programming II - Java
// Date:
// Unit # 1
// Description: This is my very first program
public class NameOfClass
{
public static void main (String [ ] args)
{
statements will be written here
System.out.println(�My first program!�);
}
}
section1.1 Commenting your code
(//) as you probably guessed is an inline comment. it is very good to use comments, because comments are simply an English explanations to help you (and other programmers) understand your program.
There is also another way to comment something out with block comments (/* */). For example:
/* This is block comment
that spans
across three lines */
section 1.2 calss name
The first line begins with the keyword class. For now, think of the word �class� as meaning program.
Class: class NameOfClass
Later we will more appropriately discuss classes, but it will be only in the ending of my totorial
The class name should be the same as the name used when creating the file. (the compiler sees the class name and looks for the file with the same name, ) this is to make your program compile.
The class name should begin with a capital letter. (this is to make your code more expressive and comprehensible )
section 1.3 main()
Main Method: public static void main (String [ ] args)
� This section of code is called the main method. (A method is simply a block or set of instructions to be executed).
� All Java applications begin execution at main (). (similar to C, C++)
whenever you start your program always copy this line in order for a program t compile
section 1.4
Statements: System.out.println(�My first program!�)
� This is the first executable line of your program. It displays:
My first program!
on the screen.
� All Java statements must end with a semicolon (;). (again C/C++ feature)
section 1.5
Compiling:
Before you execute (run) a program, you must first compile it. When the program is finished compiling you will see the message �Process Complete� or you will get a list of compiling errors that you will need to correct before you can execute the program. Once your program has compiled without any errors, then you can execute it.
section 1.6
Variables and Data Types
Variables are used to store data such as numbers and letters
� Variables can be thought of as containers that hold data
� Choose variable names that are helpful to the readers
� The name of a variable should suggest their use
� Variable names should begin with a small letter.
Numerical Data Types
There are two common numerical data types that we will use in Java � one for integers (int) and one for decimals (double).
Integers (int):
� Represents integers or whole numbers
� No commas, decimal points, or special symbols ($) are allowed
All decimals (double):
� Represents any number having a decimal point
Character Data Type
Java provides the variable type char (short for character) that can be used to store a single letter or character. Data of char type use single quotes. The Java programming language deals with strings in a very different fashion than it deals with numbers and single letter characters. You will work with strings in the next unit.
Declaring Variables:
Every variable must be declared before it is used. You can write each variable declaration on its own line or combine variables with the same data type.
Examples: int age; double amount;
int weight; double interestRate;
or
int age, weight; double amount, interestRate;
that's it for today. next article i'll start with assigning the values to variables
What would you rate this article?Please login to rate and upload coding articles. To start registering a free account with us, click here..