Benefits Of Data Types
Data types are used by programmers to store and define data, There are many different types of Data types as there are many ways in which you can store data. Data types can hold values in numbers and characters, Data types also allows developers to show how data is being stored.
There are many samples of codes, here I will explain a few.
Integer: Known as an int int the java program it is used to declare an whole number this is good for various things from age to birth year.
Boolean: Boolean is used to declare a true or false statement, in other words Boolean is a data type used to hold true or false data this cannot be used with math calculations.
Char: This is used to declare a single character it cannot hold more than a character, this is good to declare sex (M or F) and many other things.
Data types:
byte: this can hold numbers in the range off -128 to +127
short: this can hold numbers ranging from -32,786 to + 32,767
long: this can hold number in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
IF) IF statements are used a lot in java, this is a method of asking the program a question. for e.g. if you have a requested a user to use numbers instead of characters then you may use an IF to detect any characters and to tell users to use numbers, An example of an if statement....
if(sAge.contains("A")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
This is telling the program to see if the letter A is found then to send an error message to the user telling them to write in numbers not letters.
Else and Else if: Instead of using IF twice you can use IF then Else this make your program look better and more efficient. Else follows after a IF and is executed when a Boolean is expression is proven false.
Here is an example....
if(sAge.contains("A")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
}else if(sAge.contains("B")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
Else and Else if: Instead of using IF twice you can use IF then Else this make your program look better and more efficient. Else follows after a IF and is executed when a Boolean is expression is proven false.
Here is an example....
if(sAge.contains("A")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
}else if(sAge.contains("B")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
As you can see this is an Else statement this is just a shorter way for the program to read through and find an expression which proves the Boolean false. If the first condition does not work then the program will check the second one.
FOR: The for statement provides a good and compact way of repeating a range of values, this is often referred to as a for loop by programmers as it repeatedly loops until a certain condition is satisfied.
for (initialization; termination;
increment) {
statement(s)
}
This is an example of a For or for loop.
WHILE LOOP: The while loop is used to control a structure and it allows you too repeat a task a number of times. The while loop keeps repeating until the condition is satisfied, here is an example...
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
This is basically telling the program to keep looping until the value of x is near 20, this means it will only loop till the number 19.
As you can see there are many Data types and each one is unique and has its own purpose, Data types are an very important way reflecting the nature of the data stored within a program.
As you can see there are many Data types and each one is unique and has its own purpose, Data types are an very important way reflecting the nature of the data stored within a program.
No comments:
Post a Comment