Wednesday, October 22, 2014

Java- Day 2 : The ABCs of Programming

Statements and Expressions


A java program can be broken into a series of statements. Statement is a simple command that causes something to happen. A statement represent a single action taken in a Java program.

eg :- int weight = 225;
   
       System.out.println("Free the bound periodicals!");

Some statements can convey a value. eg:- addition,comparings

A statement that  produces a value is called an "Expression". The value can be stored to use later in the program. The produced vale is called "return value"

Types of return values:

             1. Numeric return value
             2. Boolean values = true/false
             3.Java object
The end of a statement and a begin of a new statement is stared with the "semicolon (:) "

     dante.speed = 2; dante.temperature = 530;

this will compile properly even there are 2 statements in a single line.
We enter only one statement in a line because of the readability of the other programmers.

statements of Java are grouped by opening brace ({) and a closing brace (}).
An organized group of statements between those braces are called a "BLOCK"


Variables and Data Types



In a variable you can change the value at anytime but not the name !
Create a variable : Identify the type of information it will store, and give a variable name.

Types of  variables


1. Instance variables - define an Object attributes
2. Class variables - define the attribute of an entire class of objects and apply to all instances of it.
3. Local variables used inside method definitions or even smaller

Creating  Variables

(variable type)_(variable name);

int- holds the integer type
string- holds text
Boolean -used to store Boolean values such as true false

Variable declaration :
   eg:-  
                     int total;
                     String reportTitle;
                     boolean active;

This creates three variables:

                    String street, city, state;

Assigning Values to the variables:    (=) is used to assign a value.

  eg:-           int num1 = 1;
                   String street = "flower road" ; 

You can assign variables like this too:  using (,) to separate each variables.

                  String city = "Kotte" , street = "Flower road" , state = "

You must give values to the local variables before you use them.
Why? : otherwise program won't compile successfully.

         
                     instance and class variable definitions are given an initial value depending on their type:

 eg:-    

  • numeric variable : 0
  • characters : '\0'
  • booleans : false
  • Objects : null
    Naming Variables


must start with a
  • letter
  • underscore character (_)
  • dollar sign ($)

should not  start with a NUMBER. 
After the first character you can include any letters or numbers.
Sometimes using the Unicode character lists. Which are in international alphabets.

Java is CASE SENSITIVE. eg:- rose is not equal to Rose
they are 2 variables.

Variables should have a MEANINGFUL name, That include joined words

RULES

  • the first letter of the variable name is simple
  • each successive name of the variable name starts with a capital letter
  • all other letters are lowercase
eg:- int  roomTemperature;
        Button loadFile;

Variable types:
  • one of the primitive data types . eg: int, boolean
  • the name of a class or interface
  • an array



Data Types

there are 8 data types:
 1.  integers- 4 integer data types
               byte     - 8 bits
               short    - 16 bits
               int       - 32 bits
               long    -64 bits
2. floating point numbers   : float, double
3. characters : char
4. boolean values : boolean

these are "signed" which means they can be either POSITIVE or NEGATIVE.
5. Ninth one is Void. -which represents nothing.
     Used in methods to indicate that they don't return a value.

Class Types

In addition to the primitive data types. A variable can take a class as its type.

eg:-

       String lastName = "Hopper";
       Color hair;
       VolcanoRobot vr;

When the variable has a class as its type it refers to an object of that class or one of its sub-classes.

You can assign values to the variables using '='.

Constants

if a value of a variable should not change during the program, its called a constant.

you can create constants for all kinds of variables instance, class, and local

to declare a constant use final keyword before the variable name.
   eg:- 
               final float PI = 3.141592;
               final_dataType_ConstantName = value ;

constants are written in all caps.

_________________________________________________________________________________

Listing 2.1

Output:

System.out.println() is a method called to display strings and other information to the standard output device.which usually is the screen. to present more than one variable r literal as the argument to println(), the + operator combines the elements into a single string...

if you want you can print all the outputs in a same line by using:
System.out.print()
in 'println' it has the command = print in a new line!


Comments

Comments are used to increase the readability of a program to the programmers.
There are 3 types of comments. 
  1. double slash (//) this is used to comment on a single line/
  2. multiple line comment. (/*            some text here  */)
  3. Javadoc comment (/**    */) :Official way of commenting used to explain how the classes and the methods are working.

Literal


Literals are the values that are assigned to the variables. These can be text, characters, numbers.

To specify a literal of float add the letter F/f to the literal as below
                        float piValue = 3.1415927f

You can use exponents in floating-point literals by using e/E
                       double x = 12e22;
                      double y = 19E-95;

Java lets you to use (_) to enter long integer numbers to increase the readability of the human,
                     int jackpot = 3500000;
                     int jackpot = 3_500_000;
           
                     
         Q: How to make 1 for true and 0 for false in boolean literals?

Character Escape Codes:

  1. \n      -new line
  2. \t       -new tab
  3. \b      -backspace
  4. \r       -carriage return
  5. \f      -form feed
  6. \\      -back slash
  7. \'       -single quotation mark
  8. \"       -double quotation mark 
  9. \d       -octal
  10. \xd     -hexa decimal
  11. \ud      -Unicode characters


String Literals

Strings are not saved in arrays as C. String is a real object in JAVA. Methods are available to modify, combine and determine whether two strings have the same value.


Strings can include the character escape codes as well. Java stores the string values as string objects. so its easier for the user to use strings as the normal primitive data types.

Expressions and Operators.


Expression is a statement that can convey a value. Some of the most common expressions are mathematical, such as the following examples. 

     int x = 3;
     int y = x;
     int z = x * y ;
value conveyed by an expression is called the return value.

Operators are special symbols used for mathematical functions, assignment statements and logical comparisons.

Arithmetic


+     addition   
 -      subtraction
     *    multiplicatio
/      division
%    modulus



No comments:

Post a Comment