In a new programming language how can store multiple types of data in a single variable OR is it possible how can store multiple types of data in a single variable in programming langauge
In a new programming language how can store multiple types of data in a single variable
OR
is it possible how can store multiple types of data in a single variable in a programming language
Here In a new programming language how can store multiple types of
data in a single variable help you make the new
concepts, you are learning a programming Languages really very important:
Ideally Yes. If you create an Array of objects. Then it's possible to store any data type with it as an object is the base class. ArrayList can hold multiple types of data, but array doesn't.
Variables
Variables can be thought of as ‘containers’ for data. Each variable has to
have a unique name, so that you can refer back to it in the program.
By using variables, we can refer to values that have not yet been defined.
For example, we can refer to the speed of the ball in different parts of the
game code, but calculate the ball speed later based on different inputs.
How a variable is named is mainly up to you, but it’s a good idea to use names
that reflect the purpose, for example, small refers to the ball in the game code.
However, there are some rules for naming variables in Java that have to be followed:
The name should begin with a letter, the dollar sign “$”, or an underscore “_”.
The name can’t be a ‘reserved word’. Reserved words are used by the Java language to refer to specific things.
For a full guide to naming variables in Java, see the variables documentation on the Oracle website.
Data types
When creating a variable, we also need to declare the data type it contains.
This is because the program will use different types of data in different ways.
Programming languages define data types differently. For example, almost all
languages differentiate between ‘integers’ (or whole numbers, eg 12), ‘non-integers’
(numbers with decimals, eg 0.24), and ‘characters’ (letters of the alphabet or words).
But, in Java there are also several ‘primitive’ data types:
char – a single 16-bit Unicode character, such as a letter, decimal, or punctuation symbol.
boolean – can have only two possible values: true (1) or false (0). This data type is useful in conditional statements, which we will cover in more detail in week 3.
byte – has a minimum value of -128 and a maximum value of 127 (inclusive).
short– has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
int: – has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
long – has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
float – a floating-point number with 32-bits of precision
double – this is a double-precision floating-point number.
The data types that hold numeric values are byte, short, int, long, float, and double.
The difference between them is the size and precision of the value they contain.
Multiple characters or ‘strings’ (eg words and sentences) are also supported in Java through the use of String.
Declaring a variable
Before using a variable in a Java program it has to be declared. Think of it
as putting a label on your container that describes the content.
Example 1
If you want to declare two integer type variables with the names
integer and MyInteger in Java it will be written as:
int integer;
int MyInteger;
This declaration comprises of 3 parts – first the data type int is declared, then
the variable name myInteger and finally the statement is concluded with a semicolon ;.
The Java syntax (or arrangement) requires us to end each ‘statement’ or
block of instructions with a semicolon.
Java variable names are case sensitive, so MyInteger and myInteger are
considered to be two different variables. To avoid confusion, it is better
practice to choose names that are more distinct.
Example 2
In this case the variable names indicate their use:
float mBallSpeedX;
float mBallSpeedY;
mBallSpeedX holds the speed of mBall in the horizontal direction (or X axis)
while mBallSpeedY holds the speed of mBall in the vertical direction (or Y axis).
It is also possible to assign values to variables in the same statement
as the declaration. For example;
float mBallX = 0;
float mBallY = 0;
This tells the program that the variable with the name mBallX contains floating
point data and that the value of this is 0 (it is situated at 0 on both the X and Y-axis).
Conclusion-In this tutorial you will have to learn In a new programming language how can
store multiple types of data in a single variable on Programming Language,
So hope you liked these tutorials. If you have any questions or suggestions related to
Programming languages, please comment below and let us know.
Finally, if you find this post informative, then share it with your friends on Facebook, Twitter, Instagram.
Thank you...
Comments
Post a Comment