Data in java programming language
Any information that is used in the program and has its place in the computer's memory is called the data.
Data in Java can be categorized as simple or complex. Simple data types represent individual pieces of data, such as numbers or characters, while complex data types, known as objects, represent data structures composed of multiple simple data elements.
If a single number can fully describe the data in binary form, we refer to it as primitive data.
If we can not describe the data using a single number, we describe it by using several items that represent free data for themselves, then it is a complex information. Complex data is called objects.
Data in Java can be categorized as simple or complex. Simple data types represent individual pieces of data, such as numbers or characters, while complex data types, known as objects, represent data structures composed of multiple simple data elements.
If a single number can fully describe the data in binary form, we refer to it as primitive data.
If we can not describe the data using a single number, we describe it by using several items that represent free data for themselves, then it is a complex information. Complex data is called objects.
Simple data
In order to reserve space in the memory for specific data, it must be known what kind of information is concerned, i.e. whether it's an integer or a decimal number, text or logical information (true-false). Therefore, each data has its own description (type) of the data.
Program command: int a;
means to allocate the space in the memory for the integer data we call a.
Here int is the official word of the Java language and indicates the whole number, while the tag (name) of the data. The name of the data is also called the data identifier because it serves to "identify" a specific data. Here are some simple types of data used in Java with the value they occupy in the memory:
Program command: int a;
means to allocate the space in the memory for the integer data we call a.
Here int is the official word of the Java language and indicates the whole number, while the tag (name) of the data. The name of the data is also called the data identifier because it serves to "identify" a specific data. Here are some simple types of data used in Java with the value they occupy in the memory:
Data type in java
The table below shows the basic data types in Java, along with their descriptions and the amount of memory they occupy:
Data type | Description | Memory[bit] |
---|---|---|
char | The integer data used for characters | 8 |
short | Short integer data | 16 |
int | Integer data | 32 |
long | Long integer data | 64 |
float | Real number in single precision | 32 |
double | Real number in double precision | 64 |
boolean | Logical data type (can be true or false) | 1 |
Video lessons
Video 1-Installation of java jdk and Netbeans IDEIn the following video, see how to install the JAVA JDK platform and the Netbeans IDE programming tool:
|
Video 2- Simply application in JAVAThis video show you how you can create an simply application which write some text on the output using Netbeans IDE
|
Video 3: Entering different data tupes in JAVA
This video show you how to write some code in JAVA which will get some data from user for inserting into the application.
Data in java example
TASK:
Input two integer data, calculate their sum and display the result on the screen
Through this example, a procedure can be shown to solve similar tasks that may be much more complex than this.
The problem solving process can generally be divided into four phases.
In the first part of the task, we need to reserve a memory for all the data that we will use in the program. Reserve memory means defining data. In this case, we have two summands that we will name a and b and the result that we will name with c. In order to reserve the memory for the first data, we have to write an instruction consisting of two words: the data type (data description) and the name of the data. For the description of the data, some of the official words are used: int, float, double, String, .. and are given in the table above. A name, a word or more words can be given as the name of the data, provided that they are written in a merged form. In this case we will use the letters a, b for the bits, and c for the result. Therefore, the orders for defining three data are as follows:
int a;
int b;
int c;
We used int as a description because this word is used when it comes to integer data (see table). In the end, the orders end with a point-comma. Reserved memory could be displayed as shown in the picture below.
In the second part of the task, the input data in this case should be given, and b. Input data is because I enter their values into the program ie. In the memory, and the data that the program accounts for us are output, in this case it is the result of c. The data is entered into the memory using the "=" assignment operator.
a = 5;
b = 7;
The first command means that the number 5 is entered into the memory that we call in our program, and the other that number 7 goes to memory b. Operator "=" does not mean the equality of the left and the right, because when the program encounters the command "a = 5", it is only empty memory and can not be "empty" to be equal to 5. This operator means that the memory will be allocated number 5 and after this command, the value will be 5 in the space (see figure below). To test whether the left side is equal to the right, the operator "= =" is used, which will be discussed in the following lessons.
Here, b and b are easy to set programmatically. This program would make more sense if it would allow the program user to enter values for a and b, for example, if desired. through some dialogue. However, here the program data set is done only for the purpose of simplifying the task. The way in which the user's communication with the program is realized will be discussed in the following lessons.
In the third part of the task, the output data should be calculated based on the formula that connects the output data (in this case c) with the input (a and b).
c = a + b;
Now, on the right-hand side of the sign, "=" is the term "a + b". It should be noted here that all data on the right side of the "=" symbol has the entered value in the memory, otherwise a compile error will be reported. The displayed command is executed by taking the value a and b from the memory first, then these values are stored (12) and then the result is stored in the memory on the left (c). After this, the memory situation is:
In the fourth part of the task, the result should be displayed on the screen. For this we will use the print (String s) method as in the previous example (Explained in the previous lesson):
System.out.print ("c =" + c);
Text must be in brackets (String data type), which is the first part that is indicated by the characters. The second part, i.e. C, which is written without an alphanumeric statement, is an int-type data which means an integer. So, here are the combined String and int and are associated with "+". Here, there will be an automatic conversion of the int-type data into a String type, so the two strings will be further assembled into one String, so that after starting the program at the output,
C = 12
It seems that instead of the letters c that actually represents the reference to the memory space at the output, the value is in that space (12) and not the letter "c". The letter c would have been that the other c was placed under the quotation marks ie.
System.out.print ("c = c"); What would be a mistake !!
Input two integer data, calculate their sum and display the result on the screen
Through this example, a procedure can be shown to solve similar tasks that may be much more complex than this.
The problem solving process can generally be divided into four phases.
- Defining (declaring) all data
- Enter or enter input data.
- Calculating output data
- Showing results and possibly other data
In the first part of the task, we need to reserve a memory for all the data that we will use in the program. Reserve memory means defining data. In this case, we have two summands that we will name a and b and the result that we will name with c. In order to reserve the memory for the first data, we have to write an instruction consisting of two words: the data type (data description) and the name of the data. For the description of the data, some of the official words are used: int, float, double, String, .. and are given in the table above. A name, a word or more words can be given as the name of the data, provided that they are written in a merged form. In this case we will use the letters a, b for the bits, and c for the result. Therefore, the orders for defining three data are as follows:
int a;
int b;
int c;
We used int as a description because this word is used when it comes to integer data (see table). In the end, the orders end with a point-comma. Reserved memory could be displayed as shown in the picture below.
In the second part of the task, the input data in this case should be given, and b. Input data is because I enter their values into the program ie. In the memory, and the data that the program accounts for us are output, in this case it is the result of c. The data is entered into the memory using the "=" assignment operator.
a = 5;
b = 7;
The first command means that the number 5 is entered into the memory that we call in our program, and the other that number 7 goes to memory b. Operator "=" does not mean the equality of the left and the right, because when the program encounters the command "a = 5", it is only empty memory and can not be "empty" to be equal to 5. This operator means that the memory will be allocated number 5 and after this command, the value will be 5 in the space (see figure below). To test whether the left side is equal to the right, the operator "= =" is used, which will be discussed in the following lessons.
Here, b and b are easy to set programmatically. This program would make more sense if it would allow the program user to enter values for a and b, for example, if desired. through some dialogue. However, here the program data set is done only for the purpose of simplifying the task. The way in which the user's communication with the program is realized will be discussed in the following lessons.
In the third part of the task, the output data should be calculated based on the formula that connects the output data (in this case c) with the input (a and b).
c = a + b;
Now, on the right-hand side of the sign, "=" is the term "a + b". It should be noted here that all data on the right side of the "=" symbol has the entered value in the memory, otherwise a compile error will be reported. The displayed command is executed by taking the value a and b from the memory first, then these values are stored (12) and then the result is stored in the memory on the left (c). After this, the memory situation is:
In the fourth part of the task, the result should be displayed on the screen. For this we will use the print (String s) method as in the previous example (Explained in the previous lesson):
System.out.print ("c =" + c);
Text must be in brackets (String data type), which is the first part that is indicated by the characters. The second part, i.e. C, which is written without an alphanumeric statement, is an int-type data which means an integer. So, here are the combined String and int and are associated with "+". Here, there will be an automatic conversion of the int-type data into a String type, so the two strings will be further assembled into one String, so that after starting the program at the output,
C = 12
It seems that instead of the letters c that actually represents the reference to the memory space at the output, the value is in that space (12) and not the letter "c". The letter c would have been that the other c was placed under the quotation marks ie.
System.out.print ("c = c"); What would be a mistake !!
Data entry in Java
If we look at the example described above, we can see that the user of the program cannot change the values of variables "a" and "b", and therefore, such a program in practical use would not make sense. What good is a program for any calculation if it always calculates one variant of the initial data? The previous program or another similar to it would only make sense if it performs a calculation (sum in the previous example) for any variant of input data entry. This will be achieved if the program enables communication with the user, such that he is asked to enter input data (a and b in the previous example). Below we will show how to communicate with the user through a Scanner class object. Read more about classes and objects on the Classes and Objects page.
Enter text data
To allow the user to enter any data, we need an object of class Scanner from the java.util package. So it is necessary to create an object in the following way:
Scanner sc=new Scanner(System.in);
We named this object 'sc' and linked it to the standard input stream, System.in. The standard input is a memory space (InputStream class) where the data that the user types when entering is temporarily stored. It is necessary to further call the appropriate method of the object loader, which takes the data from the data stream System.in and converts it into the appropriate form, e.g. in String, if it is textual data. If we enter data of type String and initialize it with the text entered by the user via the keyboard, the command that will do it is:
String text=sc.next();
Enter integer data
When entering integer data, instead of the next () method, which returns data of type String, you should call the nextInt () method of the same object of class Scanner named sc, which returns integer data of type int, as the return value of the method.
int number=sc.nextInt();
Of course, it is necessary that the object of class Scanner, "sc" be previously created. If more than one data of the same or different types is entered in the same program, it is necessary and sufficient that there is only one object to load. See what it looks like in the following example:
Example: Determining the age
Create a program that allows the user to enter their first and last name, year of birth and current year, and then calculate his age. If the variables are declared as your_name, your_lastname and your_age, print the data on the screen in the following format:
"Hello your_lastname your_name, you are your_age old".
"Hello your_lastname your_name, you are your_age old".
Solution
First you need to define two data type String and 3 integer data. Then create an object of class Scanner, "sc". When entering, you should call the appropriate methods for loading and converting the data into the appropriate format. When the name of an object is written, it is dereferenced by placing a point after the name of the object, as in the following figure:
After dereferencing the object, a list of properties and methods of the Scanner class object opens. The next () method (rounded in red) should be selected. We see that the method has no parameters because the parentheses are empty. It can also be seen that the method returns data of type String (marked with a blue line).
After entry, the age is calculated by subtracting the current and year of birth. It should be noted that this is not completely true, because the month and day of birth were not taken into account, but that would significantly complicate the task, and the goal is only to show through this task how different types of data are entered. See the complete code in the following image:
After entry, the age is calculated by subtracting the current and year of birth. It should be noted that this is not completely true, because the month and day of birth were not taken into account, but that would significantly complicate the task, and the goal is only to show through this task how different types of data are entered. See the complete code in the following image:
Before the text is printed on the standard output, the complete text for printing is formed and placed in a special variable of type String. Multiple pieces of text are joined using the "+" operator. After starting the program and entering data on the standard input, the program is executed completely as in the following figure:
Previous
|< Java lessons |
Next
Selection statements in Java >| |