Printing data and variables in JAVA Lesson |2| - ProgrammerTech
Language:

Search

Printing data and variables in JAVA Lesson |2|

  • Share this:
Printing data and variables in JAVA Lesson |2|

 

Printing in Java

Printing in Java, there is no doubt that learning to print is one of the first lessons in any language we learn because it is the output of any code we see in front of us.

We will open the program and create a new project with it, and we will delete the comments that are in the code when creating a new project.

package gradleproject223;
public class JavaApplication {
    public static void main(String[] args) {
        System.out.println("Programmer Tech");
    }    
}

 

structure of Java program

Java is based on the structure of object-oriented programming (oop), and by looking at the previous example, we note the beginning of the program. package, then the name of the package, and this is the name that appears. In the dialog at the beginning of the project creation, the word package means a package.

And the package contains within it many classes and many functions and variables so that this package can be used in another place or another project.

Then the second line is always written public class JavaApplication when we are programming. In oop we write a class and then the name of the class and inside the class there is. Many functions including the main program and the word public means can be returned.

Classes can be reused from outside the package. We have access degrees public, private, and protected. In general, we can use any variable or function from outside the package.

We notice in the third line written public static void main(String[] args) This is the main program.

And from the main program, the program is launched and the commands are executed according to their sequence from the main program.

The meaning of public is that we talked about it and we said that any function or variable can be used from outside the class, unlike private and protected, and the meaning of static means. We can call this function by the name of the class and not by the object.

Any function or any variable defined as static can be called by the name of the class.

The word void means that this function does not return a value and main means the main program and after it String[] args These are parameters for sending text parameters from outside the class.

In our previous program, we learned how to print the word Programmer Tech using the function. System.out.println and then we wrote the word we want to print within the parentheses and then we execute the program to start executing and print the sentence.

 

Java variables

java variables, we can define simplified as a place to store a value temporarily. It reserves a certain space in memory.

And we said temporarily because the variable loses its value once you exit the program or terminate the procedure.

We will notice that variables will be used in every project and in every program we must use variables.

 

defining a variable in java

To define a variable in the Java language, we first write the type of the variable, then the name of the variable, then a semicolon, and the general form of it is as in the figure.

type var_name;

 

Types of variables in Java

  1. Integer integer variables reserve 4 bytes of space in memory.
  2. Double long decimal integer variables reserve 8 bytes of space in memory.
  3. String text variables.
  4. float and reserve 4 bytes of memory space.
  5. Variables store a single char and reserve 2 bytes of space in memory.
  6. Boolean variables that accept either true or false.
  7. Byte variables accept an integer value and reserve 1 byte space in memory.
  8. Short variables accept an integer value and reserve 2 bytes of space in memory.
  9. long variables accept an integer value and reserve 8 bytes of space in memory.

 

integer variables in java

These variables include integer values ​​i.e. numbers such as 8,5,23,0 .... etc.

int c;
d=23;

We created the variable d at the beginning and then assigned it the value 23, and we can assign the value to it directly when writing it as follows.

int s=6;

It is not possible to create two variables with the same name and type in programming languages, as we noticed in the first we used c and the second we used s and we can also assign a new value to the variable.

int s=23;

Here the variable s has lost its old value of 6 and has been assigned a new value of 23.

package prt;
public class Prt{
     public static void main(String[] args){
         int c;
         c=23;
         int s=6;
         int s=23;
    }
}

Example:

Define three variables of type int, assign values ​​to them, add them, and print the result on the screen.

package prt;
public class Prt{
     public static void main(String[] args){
         first_number = 23;
         second_number=27;
         answer = first_number + second_number;
         System.out.println("Addition Total = " + answer );
    }
}

 

double decimal variables in java

The variable double comes greater than int and greater than float and represents a number with a decimal point of 64 bits, if we do not set a value the default value is 0.0d.

double c=23.6;

 

String variables in Java

If we have a text value and we want to store it in a variable, this variable must be of type String, and the value must be within two quotes.

And the variable definition word is case sensitive. The first letter must be written uppercase. If we write it lowercase, it will not be recognized.

String c="ProgrammerTech";

We can combine two text strings by defining a third variable and assigning the first two values ​​to it.

String d="ahmed";
String a="data";
String t=d+a;

We create two variables and then merge them with a third variable t.

 

float variables in java

This includes a fractional or real number, and represents a number with a decimal point consisting of 32 bits less than double. If we do not set a value, the default value is 0.0f.

float c = 14.45f;

 

Variables to store a single char in Java

It includes letters in all their forms, and only one letter is stored in it, and it is within vocabulary quotation marks. If we want to create a variable to store a single letter, we use char.

char c = 's';

 

boolean variables in java

These variables are called logical variables. They take only one of two values, which are true or false. They are very important in the programming world and programmers use them frequently.

boolean c = true;

It is not important to assign a value to the variable when defining it, you can assign a value to it whenever you want anywhere in the program

 

Defining variables of the same type in Java without values

If we want to define several variables of the same type, but we do not want to assign any values ​​to them, it will be of the form.

int saja;
int kaissar;
int muhammad;

To shorten the program, we can use these same variables on the same line as follows

You are shrouded, Caesar, Muhammad;

 

Defining variables of the same type in Java with values

int saja = 27;
int kaissar = 22;
int muhammad = 28;

To short the program, we can use these same variables and give them values ​​on the same line as follows

int saja=27, kaissar=22, muhammad=28;

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com