Variables in Java Context: Validity level, Object Variables, Wrapper Classes, Class variables

Variables in larger Java context:

Validity Levels in java Variables:

Let’s start by asking in which code area variables in java are valid. The rule is somewhat simplistic: Variables in java are available in all code levels visible within the declaration layers, but not in the layers outside. Code layers are formed by curved brackets, for example, when formulating classes, methods, queries, or loops. Syntactically, it is also allowed to level with curved brackets without forming a concrete function. The following example declares the variable x in the second level. The variable can be in the inner third level as well as in the second level. In the outer, first plane, however, it is not accessible. The Java compiler returns the error cannot find symbol. To resolve the error, you must either move to the You can omit variable access or you must delete the declaration of x from the second level to the first level.



Object Variables in java:

Often, Variables in java should not use elementary data types (int, double, etc.), but objects. Our problem at this point is, that you don’t know what objects actually are. Let’s settle at this point, objects have concrete formations (instances) of a class, a class in turn being a kind of blueprint for a is its own data type. At first glance, this does not change much: Instead of an elementary Data types, you now specify the class name when declaring on. An example is the class Point for storage two-dimensional coordinates. This class is available in the Java library in the package java.awt defined.

java .awt.Point p1;

The first difference is the initialization of the variable. Without explicit Assignment contains p1 state zero. Unlike Variables in java for elementary data types can be read p1 without explicitly specifying a Assign. Of course, you have declared p1 to have a coordinate point file. This leads to the second difference: The corresponding object you must create with new explicitly:

p1 = new java .awt.Point(2, 3);

Because objects are derived from classes, the objects fields and methods. For example, you can access the X component of the coordinate point with x. The + within the println method causes the X coordinate to be converted into a string and the text X-coordinate: is added:

System.out. println(“X-coordinate: ” + p1.x);

But the fourth and most fundamental difference concerns the Handling the data. For elementary data types, the data directly in the variable. When assigned, the data, which is obvious:

Variables in java, on the other hand, store a reference to the data. The variable p1 is in a sense only a pointer to the location in the memory, where the object data is actually stored. This has a great Impact on assignments! Only the reference. In the following example, p1 and p2 refer to the same data. Changes therefore affect both Variables in java. The following For example, p1 and p2 refer to the same Point object. change the X-coordinate, this change also applies to p1!


Example: how to use object class variables in java:

OutPut:

As you will see in my next article, “Methods”, this basic Behavior difference between elementary data type Variables in java and object Variables in java also for the parameters of a method. If you need an independent copy of an object, you must copy or duplicate (clone) the object. Some classes of Java For this purpose, the standard library provides for and implements the clone method the Cloneable interface. Note, however, that clone only direct object (shallow clone). If the object is objects and also those objects should be duplicated requires a deep clone, which in Java, for example, requires serialization methods is enabled. In general, neither the clone method nor the Cloneable interface in Java circles. The Java chief developer Joshua Bloch advises from implementing Cloneable and clone in its own classes; and recommends that you develop your own copy constructor instead. For handling simple point objects clone is enough to make at p3 independent of p4:

Wrapper Classes Elementary Data Types in :

Wrapper classes are also available in Java for all elementary Java data types (see below Table ). These are classes that are elementary Package data types.

 

Data Type Boxing variant
byte Byte
short Short
int Integer
long Long
boolean Boolean
char Character
double Double
float Float

 

In contrast to elementary data types, their wrapper Variants also take the value zero. That’s particularly handy. if not initialized between 0 and state, the Commission should make a distinction. Wrapper data types are also suitable for database applications because they can map the SQL value to NULL. Finally, you can use wrapper data types if you store elementary data in collections.


Note:

Use wrappers with care!

You should only use the wrapper data types if you use them as described in the scenarios listed above. In all other For efficiency reasons, you should set the elementary data types to the Give preference.

Although wrapper classes are real classes, numbers can be used without the use of new. The transformation between elementary data types and their wrapper variants, i.e. boxing and unboxing is done automatically. Explicit casting is not required.

Integer i=3;

int j = i + 1;

i = j + 1;

Wrapper classes can thus be used much like elementary data types in the following. However, take note of the special cases that this may can show that the state is zero allowed:

Integer i=null;

int j = i + 1; // throws NullPointerException

The wrapper classes are defined in the standard Java library java.lang and are available in all Java programs without import. They contain a set of useful conversion methods that can be used even then can be used if you have ordinary elementary Use data types.

double d = double. parseDouble (‘12.34’);

string s = Long.toHexString (123); // 7b



Class variables in Java:

In all previous examples, Variables in java within a method were file. However, it is also possible to set variables on classes or type level. Such Variables in java are called fields in English, in this article, class Variables in java. Many details and examples to declare and use such Variables in java follow in my next article, “Classes”. There you will also find keywords private, public and protected. They shall determine: whether and under what circumstances code outside the class access to the class Variables in java.

Note:

Readability versus Accuracy

Unfortunately, there is a really optimal German translation of Fields not. Fields is a term that is too common, often not Fields, but Arrays means. Instead, the name is usually not exactly the same Class variable. However, fields can be declared not only in classes, but for example, also in enumerations. The generic term for classes, enumerations, interfaces, etc., is type. This would result in class Variables in java Type variables. But that doesn’t help us either, because this term usually refers to the Variables in java in generic classes; there is the data type itself is variable. And so in this article, I stick to the Term class variable.


Related Article:

Java Literal: Boolean, whole Numbers, Floating point numbers

Feature of Import Class In Java With Examples

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: