Java

[On To Java 2] Chapter 3: How to declare variables

jnk1m 2023. 1. 16. 16:24

59. A variable is a chunk of computer memory that contains a value. The name of a variable is an identifier that refers to the variable.

 

A variable's data type determines the size of the chunk and the way that the bits in the chunk are interpreted. If the data type of a variable is int, the variable holds a 32-bit signed integer. If the data type of a variable is double, the variable holds a 64-bit signed floating-point number.

 

As program runs, a variable's value may change, but a variable's data type never changes. Thus, the value of a variable named script, with type int, could be 8 at one time and 9 at another, but the value of script could never be a number with type double, such as 8.5.

 

60. Because every variable is typed, the Java compiler can allocate a memory chunk of the right size for each variable, once and for all, taking advantage of the fact that the value of the variable always will fit within the allocated memory chunk.


61. When you tell the Java compiler the type of a variable, you are said to declare the vari-able. Thus, the following program fragment exhibits three variable declarations. All three variables are declared to be integer variables, because each is preceded by the data-type-declaring int keyword:

public class Demonstrate {
public static void main (String arg[]) {
	int script; 
    int acting;
	int direction;
   }
}

 

63. Storing a value in the memory chunk allocated for a variable is called variable assignment.
Accordingly, whenever Java places a value in such a memory chunk, the variable is said to be assigned a value, and the value is said to be assigned to the variable.

 

64. You can initialize a variable in the same statement in which the variable is declared:

public class Demonstrate {
	public static void main (String arg[]) {
		int script = 6;
		int acting = 9;
		int direction = 8;

	}
   }

or

 

public class Demonstrate {
	public static void main (String argv[]) {
		int script = 6, acting = 9, direction = 8;
	}
}

 

💡 Then what is the difference between variable assignment and initialization?

Check out this post 

 

Java: define terms initialization, declaration and assignment

I find the defs circular, the subjects are defined by their verbs but the verbs are undefined! So how do you define them? The Circular Definitions initialization: to initialize a variable. It can...

stackoverflow.com

So basically, assignment is throwing away the old value of a variable and replacing it with a new one. Initialization is a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.

 

👉  Assignment is a more general change in value, initialization a special type of assignment. It's a change from a starting value.


Highlights

  • A variable is an identifier that names a chunk of memory
  •  The floating-point data types are float and double.
  • Integral, floating-point, and boolean data types are said to be primitive data
    types.
  • If you wish to write a one-line comment, then introduce that comment with //.
  • If you wish to write a multiline comment, then delimit that comment /* • .. */.
  • If you wish to introduce a variable, then you must declare the data type of that variable in a variable declaration:
    data type variable name
  • If you wish to provide an initial assignment for a variable, then you can include that initial assignment in the declaration statement:
    data type variable name = initial-value expression;
  • If you wish to assign a variable after that variable is declared, then use an assignment statement:
    variable name = new-value expression;

 

determine [ditə́ːrmin] 〈사람·물건이〉 〈사물을〉 (신중하게) 결정하다, 한정하다, 정하다; 〈이유를〉 명확히 하다; 
[determine+wh-절/determine+wh- to do]〈…할 것인가를〉 정하다.  
cause (something) to occur in a particular way or to have a particular nature

interpret [intə́ːrprit] …의 뜻을 밝히다, …을 설명[해명]하다 …을 해석[이해]하다 〈프로그램을〉 기계어로 변환하다.