|
Basic data types
| Data Type |
Wrapper Class |
Size |
Value range |
|
byte |
java.lang.byte |
8-bit (1-byte) |
-128 to127 |
|
short |
java.lang.Short |
16-bit (2-byte) |
-32,768 to 32,767 |
|
int |
java.lang.Integer |
32-bit (4-byte) |
-2,147,483,648 to 2,147,483,647 |
|
long |
java.lang.Long |
64-bit (8-byte) |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
|
float |
java.lang.Float |
64-bit (8-byte) |
4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative) |
|
String (object) |
java.lang.String |
Unlimited |
|
|
char |
java.lang.Character |
16-bit (2-byte) |
0 to 65,535 |
|
boolean |
java.lang.Boolean |
8-bit (1-byte) |
true and false |
When we declare a variable we assign it an identifier and a data type.
For Example String message = “hello world”
In the above statement, String is the data type for the identifier message. If you don’t specify a value when the variable is declared,
it will be assigned the default value for its data type.
Identifier Naming Rules
- Can consist of upper and lower case letters, digits, dollar sign ($) and the underscore ( _ ) character.
- Must begin with a letter, dollar sign, or an underscore
- Are case sensitive
- Keywords cannot be used as identifiers
- Within a given section of your program or scope, each user defined item must have a unique identifier
- Can be of any length.
Example:
public class Main {
/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int iCount; //declaring an integer variable char cLetter; //declaring an charecter variable iCount = 10; cLetter = 'L'; String strName = "Easy Steps"; System.out.print(strName); }
}
User Defined Java Data Types
Class
Primitive data types are not sufficient. In the real world, we have much more complicated objects. Object oriented programming allows us to model real-world objects. User defined classes combine the data and methods that operate on that data.
-
Class is a collection of member variables of primitive and/or composite data types and member methods or functions capable of accepting argument(s) of different primitive and/or class types. Each method can either return a value of specified return data type or no output value as indicated by void. Advantages:
- Class is responsible for the validity of the data.
- Implementation details can be hidden.
- Class can be reused.
- The client of a user-defined class is the program that instantiates instances of that class, and calls the methods of that class.
Data within user-defined classes should be private to that class, and clients of that class should use the setter and getter methods to access that data.
Syntax:
Class { instance variable1;
…
…
instance variableN;
method(parameter-list1)
{ Method1 body; Return …; }
…
… methodZ(parameter-listZ) { MethodZ body; Return …; } }
Access modifiers control access to this class. There are many ways in which a class can be accessed, such as:
- Methods of the user-defined class.
- Methods of other classes, outside of the user-defined class’s package.
- Methods of subclasses that inherit the user-defined class.
- Methods of classes in the same package.
Access modifiers:
- public – methods of the same class and methods of other classes.
- private – methods of the same class only.
- protected – methods of the same class, methods of subclasses, and methods of classes in the same package.
- none – methods in the same package only
Example:
public class Bicycle { // the Bicycle class has three fields public int cadence; public int gear; public int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; }
} Structure
Program Execution Control Statements
Selection Statements
if statement
This is to execute a single statement or a block of code, when the given condition is true
and if it is false then it skips if block and rest code of program is executed.
Syntax:
if(conditional_expression) {
; ...; ...;
}
Example:
public class Main { public static void main(String[] args) { // TODO code application logic here int n = 10; if(n%2 == 0) { System.out.println("This is even number"); } } } if-else statement
The "if-else" statement is an extension of if statement that provides another option when
'if' statement evaluates to "false" i.e. else block is executed if "if" statement is false.
Syntax:
if(conditional_expression) { <statements>; ...; ...; } else {
<statements> ;
....;
....;
}
|