Give example program using data type?
Share
Give example program using data type?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
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 mBall 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.
Explanation:
brainly