A Java program consists of code written in the Java programming language, which is then compiled into bytecode. Bytecode is an intermediate code that can be executed on any platform with a JVM - Java Virtual Machine. When a Java program is run, the JVM loads it into memory and executes it. During execution, the JVM uses various processes to run the program, including a bytecode interpreter and an optimizer for faster code execution. One of the key aspects of a Java program is its object-oriented nature. This means that programs are written using objects and classes defined in the code. Objects are instances of classes, and classes define the attributes and methods that describe the behavior of an object. Java programs typically consist of multiple classes that work together to perform a function.
When the program is started, the JVM first loads the main class and executes its `main` method. Other classes are loaded as needed to execute the program. Another important feature of Java programs is memory management. Java uses a GC - Garbage collector mechanism, which automatically removes unused objects from memory to free up space for other objects. This process is automatic, so as a Java programmer, you don't need to worry about memory deallocation directly. If you're interested in learning more about the Java programming language, click here. Assuming you're not a complete beginner in programming and may have some experience with the C# programming language, you'll best understand how the Java programming language works by paying attention to the differences between Java and C# in terms of their implementation and operation, despite the fact that these two programming languages share many similarities.
The Java programming language is designed as a platform-independent language that can run on any platform with a JVM - Java Virtual Machine, while the C# programming language relies on the .NET framework. Memory management in C# is slightly different; C# has a mechanism similar to the GC - Garbage collector called the .NET Garbage Collector, and the programs are compiled differently. Additionally, C# has an intermediate code similar to bytecode called CIL - Common Intermediate Language. Although both languages are object-oriented, there are differences in language aspects such as syntax, conventions, access modifiers, and more.
If you're coding desktop applications, we recommend using C#. However, if we're talking about web applications or large projects and systems that run on the Internet, Java is definitely a better choice. Overall, if you know C#, learning the Java programming language won't be too complicated for you. As for variables, the rules are the same. Variables are declared by specifying their name and data type. The variable name can contain letters, numbers, and underscores, but it cannot start with a number or contain spaces. It's important to give variables clear and descriptive names so that their purpose in the program is easily understood later.
However, when it comes to data types in Java, both languages have strongly typed data types, but there are definitely some differences. Even basic types that seem the same in both languages are implemented differently. For example, in Java, the `String` data type is written with a capital letter, while the `boolean` data type is written instead of `bool`, which beginners often confuse. Declaring variables is the same as in C#, but note that the `String` object in Java expects double quotes " ", and single quotes ' ' are used for the `char` type. In the Java programming language, you don't have data types like `dynamic` or `var`. Java has fewer data types but introduces some like `enum`. So, you'll need to adjust some habits, regardless of the completely different environment, community, and resources.
Data Types in Java: Differences and Applications
If you ask a C# programmer how data types are shared in the C# programming language, you will get a clear and precise answer that includes value and reference types. However, in the Java programming language data types are divided into two basic types: simple types and complex types. Simple types include boolean, byte, char, short, int, long, float, and double data types, while complex types represent strings that can be byte, short, int, long, float, double, boolean, char, or object strings, such as and objects like String, Date, Random, Scanner, Math etc.
Take a look at the list to see how data types are categorized in the Java programming language.
Primitive Types
·
Integer
-
byte
-
short
-
int
-
long
·
Floating-Point
-
float
-
double
·
Character
-
char
·
Boolean
-
boolean
Complex Types
·
Arrays
·
Objects
We hope this gives you a better understanding of Java data types. Next, we will create a program that will further illustrate the different data types and their minimum and maximum values, which we can use in programming and calculations. Open your IDE – the integrated development environment you use, name the new program `variables`, and enter the following Java code.
package com.tutorial;
import static java.lang.System.*;
public class variables {
public static void main(String[]
args) {
out.println("\t\tDATA TYPES IN JAVA PROGRAMMING LANGUAGE\n");
//
This boolean type of variable can be true or false
boolean booleanVariable = true;
out.printf(" boolean or java.lang.Boolean ... %s, %s %n",
booleanVariable, !booleanVariable);
//
This byte type of variable has a range from -128 to 127
byte byteVariable = -128;
out.printf(" byte or java.lang.Byte ... %d, %d %n",
byteVariable, byteVariable
= 127);
//
This short type of variable has a range from -32 768 to 32 767
short shortVariable = Short.MIN_VALUE;
out.printf(" short or java.lang.Short ... %d, %d %n",
shortVariable, shortVariable
= Short.MAX_VALUE);
//
This int type of variable has a range from -2^31 to 2^31-1
int intVariable
= Integer.MIN_VALUE;
out.printf(" int or java.lang.Integer ... %d, %d %n",
intVariable, intVariable
= Integer.MAX_VALUE);
//
This int type of variable has a range from -2^63 to 2^63-1
long longVariable = Long.MIN_VALUE;
out.printf(" long or java.lang.Long ... %d, %d %n",
longVariable, longVariable
= Long.MAX_VALUE);
//
This float type of variable has a range from 1.4E-45 to 3.4028235E+38
float floatVariable = Float.MIN_VALUE;
out.printf(" float or java.lang.Float ... %s, %s %n",
floatVariable, floatVariable
= Float.MAX_VALUE);
//
This double type of variable has a range from 4.9E-324 to
1.7976931348623157E+308
double doubleVariable = Double.MIN_VALUE;
out.printf(" double or java.lang.Double ... %s, %s %n",
doubleVariable, doubleVariable
= Double.MAX_VALUE);
//
This char type of variable has only one character
char charVariable = 'A';
out.printf(" char or java.lang.Character ... %s, %n",
charVariable);
//
This String type of variable holds a sequence of characters
String stringVariable = "A
string can have 2GB or over 1 billion characters.";
out.printf(" String or java.lang.String ... %s, %n",
stringVariable);
//
This Object type of variable can hold any type
Object objectVariable = "An
object can hold any type.";
out.printf(" Object or java.lang.Object ... %s, %n",
objectVariable);
//
a constant
final double PI = 3.14159265359;
out.printf("You cannot change constant value after
initialization.%n Constant PI = %s always.%n", PI);
}
}
When you execute the specified Java program, the result will
be this.
C:\Users\manue\.jdks\openjdk-20\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.2\lib\idea_rt.jar=10183:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\JavaProjects\variables\out\production\variables com.tutorial.variables
DATA TYPES IN JAVA PROGRAMMING
LANGUAGE
boolean or java.lang.Boolean ... true, false
byte or java.lang.Byte ... -128, 127
short or java.lang.Short ... -32768, 32767
int or java.lang.Integer ... -2147483648,
2147483647
long or java.lang.Long ...
-9223372036854775808, 9223372036854775807
float or java.lang.Float ... 1.4E-45, 3.4028235E38
double or java.lang.Double ... 4.9E-324, 1.7976931348623157E308
char or java.lang.Character ... A,
String or java.lang.String ... A string can
have 2GB or over 1 billion characters.,
Object or java.lang.Object ... An object can
hold any type.,
You cannot change
constant value after initialization.
Constant PI = 3.14159265359 always.
Process finished with
exit code 0
And if you paid attention to the code, that final double is not a variable, but
a constant.
// a constant
final double PI = 3.14159265359;
In the C# programming language, you would write the
constant like this.
// a constant
const decimal PI = 3.14159265359M;
In this example, PI represents the name of a constant of type decimal, which is initially set to the exact value of the number 3.14159265359. As is already known, after initialization the value of the constant PI cannot be changed in any part of the code, unlike variables. Constants are usually used when it is necessary to ensure that the value of a variable remains unchanged during program execution. In this case, PI is a constant value often used in mathematical calculations, although the exact value of PI may be limited by the number of decimal places that can be displayed in the program.
Using constants makes work easier because it ensures
that the exact value of PI will not change during program execution. In
addition, using the constant PI in the code instead of a whole string of
numbers makes the code more transparent and easier to read. If you need the
exact value of PI in your code, you don't need to write the entire value every
time it appears, but simply use the constant name PI. You can see how this
program is coded and how it works in the video.
No comments:
Post a Comment