Sunday, July 27, 2014

Java Basics 101- Java Variables

---

ATTENTION!

An updated version of this article is available at http://javadevsteps.blogspot.com/2014/08/ideone-java-basics-101-getting-started.html


---
Java Basics 101 – Variables

STEPS

1) Browse  http://ideone.com/VOBd7C 

IdeOne.com is an Online Compiler that allows Java Code to be written and compiled online.
We would be able to learn a lot from here without even have to install Java Development Kit to our PC.

2) Understanding Java Code Structure

2-1) Package Name
2-2) Import Declarations
2-3) Class Declaration
2-4) Method Declaration
2-5) User Instructions

3) Basic Instructions – Variable Declaration and Print Statement

As a quick start, ignore Package Name, Import Declarations and Class Declarations first.
Let’s look at basic program instructions first (Line no. 12).
Replace line No.12 with the following codes:

int first_number;
System.out.println("My First Project");
Outcome:
Lesson:
- Variable is identified by its name and data type.
- Printing statement returns value to user. (in the above example it is a string)

4) Variable Assignment



public static void
 main(String[ ] args) {
int first_number;
first_number = 10;
System.out.println("My First Project");
}
Outcome:
Lessons:
- Variable can store value; a process called assignment gives a value for variable to store.

5) Print Variable Value

Change line no. 15 to the following:

System.out.println( "First number = " + first_number );
Outcome:
Lessons:
- We can combine string with variable.
- We join string and variable with join operator “+”; a process called Concatenation.

6) Variables and Data Operation

Change line no. 13 to the following:

int first_number, second_number, answer;
Change line no. 14 to the following:

first_number = 10;
second_number = 20;
answer = first_number + second_number;
Change line no.15 to the following

System.out.println("Addition Total = " + answer );
Outcome:
Lessons:
- You can declare many variables of the same daya type in a single statement.
- Number operation is similar to that of Mathematics.
---
Moderated from: http://www.homeandlearn.co.uk/java/java_int_variables.html
---

2 comments: