Showing posts with label variables in java. Show all posts
Showing posts with label variables in java. Show all posts

Friday, March 1, 2024

VARIABLES IN JAVA

 Variables in Java

  • WHAT ARE VARIABLES 
  • TYPES OF VARIABLES
  • DECLARING VARIABLES IN JAVA 
  • INITIALIZATION OF VARIABLES IN JAVA 

Data is not stored directly, but as numbers or names to help identify data at different stages of programming. These numeric names that store data in a computer program are called variables in Java. This blog will help you understand the concept of variables in Java and its different types.

What are variables?

Variables are internal memory components of Java. They can be understood as places of memory. A variable can be thought of as a single memory location or as a collection of locations that cooperate. Variables in a program can be used to find data. They don't really describe the data; they just tell the programmer where certain pieces of data are stored in memory.

Simply put, Java variables are names used to refer to data stored in memory. A single Java variable can hold only one type of data. A variable must be declared before it can be used in programming. This means assigning data to a specific memory and using a name for that memory. We also need to specify a Java data type for the declaration. This is done using the "dataType variableName" syntax.

A computer program's values are kept track of as it runs in "variables," which the programmer can access at a later date.

Types of variables in Java?

     1)  Local variable

These kinds of variables are defined inside constructors, procedures, or blocks and are only used within those particular contexts. A local variable cannot be accessed from outside of a method. In Java, method descriptions are surrounded by square brackets. The term "method" or "block" refers to the area that is between the curly brackets ({....}).

This establishes the usage cases for local variables. It is only possible to use local variables while the program is operating. It is not possible to utilize a local variable with an external variable. A local variable is set inside a block or method, and it is deleted after the block. Remember that a local variable cannot have a default value. Moreover, access variables cannot be used to specify local variables.

 2)  Instance variable

Classes and subroutines are two more essential components of Java. A class is any group of objects that share certain characteristics. It may be used to define methods, objects, and data types. Additional divisions of the class are subroutines or methods. As you previously knew, local variables are an essential part of procedures and subroutines.

However, certain variables are not subroutine variables; rather, they are class variables. Thus, the terms "member variables" or "instance variables" refer to them. They have default values set. A class's instance variable may be defined either before to or after to its use. All instance variables (private ones excepted) are visible to all methods and constructors of the class.

 It is possible to have both static and non-static member variables. The static, public, or private modifiers are needed for instance variables to be defined.

An explanation of the differences between instance variables and local variables is provided below. The first is located inside a class's method or subroutine, whereas the second is located inside the class but outside of the block or method.

 Local variables can only be used for one function before being deactivated. They have to be carried out in tandem with this process. However, instance variables can be utilized in several functions. Even if an instance variable is kept in a subroutine, its value can still be retrieved in other subroutines.

 3) Static variable

In Java, you may prefix strings, programs, and variables with the static keyword. Java memory management makes use of this. If a component has the word "static" attached to its name, it is regarded as a part of the class as a whole, not just the object itself. The values of static variables remain the same for all objects.

 All class objects share a single static variable. Consequently, they can be called class variables or global variables. When the entire class is loaded into memory, the programmer only needs to allocate memory for static variables once.

Static variables are restricted to class members and cannot be specified as methods or subroutines. They cannot be used outside of that class since they are declared within of it.

We will then discuss yet another type of auxiliary Java variable to go along with the previously discussed ones.

 4) Final variable

Java allows you to use static variables as constants by simply applying the final keyword to them. Finite variables are the word used for them. As the name suggests, the value of a final variable is fixed and cannot be changed. However, the information included in the object may be changed.

We are unable to override a final variable in any subclass. A final variable cannot be extended or inherited by any class. If the coder changes the value of the last variable, a compilation fault will be raised. Thus, a variable must be initialized before it can be considered final. You can initialize a variable at the time of declaration. It is called a final empty variable when it has no value.

What are declaring variables in Java?

It is not possible to utilize a variable that is not defined. In Java, the process of generating a variable is called declaring one. Before creating a variable, though, it's imperative to input the data type. There is a specific amount of RAM set aside for variables. This memory location is then identified using a variable name.

It is essential to declare the data type before declaring the name to guarantee that there are sufficient memory bits available to retain the value of the variable. Entering the data type is the initial step in defining a variable in Java. Next, enter the name of the variable, followed by a space. Within a function, variables can be defined at any point.

·   When declaring variables in Java, bear the following in mind: - You can start a variable name with an alphabet, a dollar or underscore symbol, or a currency symbol instead of any other special sign.

  • ·       There is a 64-character restriction on the variable name.
  • ·       Void cannot be used to define a variable.
  • ·       You cannot utilize Java-reserved keywords in variable names.
  • ·       The variable name must appear to the left of the assignment operators.

What is the initialization of variables in Java?

When a variable is declared, it is given an initial value that can be changed later. The Java technique for generating new variables and assigning default values is called variable initialization. Before a variable can be used in a method, it has to be initialized. We are unable to use a variable without a value. A variable can be initialized as soon as it is defined. If initialization is not done beforehand, the variable is assigned a value using the assignment statement. If you attempt to print a variable without initializing it, an error will be raised.

There are explicit and implicit ways to initialize variables. The process of declaring a variable with a value already allocated to it is known as explicit initialization. If you don't initialize a variable, the system will assign a random value when the program starts. Implicit initialization, on the other hand, occurs when a variable obtains its value later on in the processing flow. This results in an unexpected consequence.

Wednesday, February 21, 2024

DATA TYPES IN JAVA

Data types in Java


  • WHAT IS THE DATA TYPES
  • TYPES OF DATA TYPES

According to data, variables can have a wide range of sizes and values. In Java there are mainly two different data types:

Basic data structures - The following are the most fundamental types of data storage: boolean, character, byte, int, long, float, and double.

Non-primitive data types: Non-primitive data types include classes, interfaces, and arrays.

Primitive data types

Java's data manipulation is based on primitive data types. These are the most basic data types in Java.

There are 8 types of primitive data types in Java: -

  • Boolean data type
  • byte data type
  • char data type
  • short data type
  • int data type
  • long data type
  • float data type
  • double data type

 Non-primitive data types

There are a greater number of primitive data types like: -

     · String

  • Array 

Boolean data type

In a Boolean data type, there are only two possible values that may be represented: true and false. This data type is perfect for basic flags with true or false criteria.

Boolean data types describe themselves as having a single piece of information; its "size" is not indicated.

Example: - Boolean one =false

Byte data type

The byte is among the most fundamental types of data. This is a signed integer since it has eight bits and the two's complement. Its possible values range from -128 to 127. It has a minimum value of -128 and a maximum value of 127. It defaults to 0.

The Byte data type is invaluable for large arrays in terms of memory conservation. This helps save space since bytes are four times smaller than integers. Being used as a "int" data type is an additional potential use.

Example: - byte a =10, byte b = -20

Short data type

Signed integers with two scores are condensed to 16 bits. It can have a value between -32,768 and 32,767. It can have any of the following values: the whole numbers -32768 to 32767. By default, its value is zero.

You can utilize the short data type in the same way as you would bytes to save memory. A short data type is one that is twice as tiny as an integer.

Example: - short s=10000, short r = -4000

Int data type

A signed, two-coordinate integer with 32 bits is represented by the data type "int". Values between -2,147,483,648 (-2^31) and 2,147,483,647 (2^31 -1) are contained within the range. Its possible values range from -2,147,483,648 to 2,147,483,647. It has a default value of 0.

The int data type is usually used as the default data type for integral values unless there are memory problems.

Example: - int a =50, int b= -80

Long data type

An integer having 64 bits of two's complement is referred to as a long data type. Its value is conceivable between -9,223,372,036,854,775,808 (-2^63) and 9,223,372,036,854,775,807 (2^63 -1) inclusive. The range of potential values is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. By default, it is set to 0. When it doesn't cover the range of numbers you require, you can use the long data type.

Example: - long a =400L, long b=-200L

Float data type

For floating-point computations, a 32-bit IEEE 754 floating-point number is utilized as the data type. There is no range to its value. Use float rather than double when working with large arrays of floats to save memory. Coins and other exact numbers should never be kept in float variables. It has a default setting of 0.0F.

Float a=490.3, float b=598.4

Double data type

The data type is an IEEE 754 floating-point number with 64 bits of precision. Its value is infinite in scope. Similar to float values, decimal numbers are frequently stored as double data types. Not to add, never use the double data type to record exact quantities, such as coins. It has a default value of 0.0d.

Example: - d =34.6

Char data type

The Char data type represents one 16-bit Unicode character. Its value range is '\u0000' (inclusive, or 0) to '\uffff' (inclusive, or 65,535). Programmers use the Char data type to store text.

Example: - char letter a =’a’

Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular