Understanding Java Variables: Types, Naming Rules, and Type Conversions
This article explains Java variables, covering their definition, data types (primitive and reference), naming conventions, value assignment, automatic widening conversions, explicit casting, and string‑numeric conversions, illustrated with clear code examples.
In Java, a variable is a mutable storage location, analogous to a plate that can hold different dishes at different times; the article begins with this analogy and shows a simple example of storing the string "清炒黄瓜" in a variable.
The standard syntax is DataType variableName = value;, and three essential aspects are highlighted: data types, variable names, and values.
Data types are divided into primitive (value) types—byte, short, int, long, float, double, char, boolean—and reference types, with String being the most commonly used reference type.
Multiple types exist to represent different kinds of data, just as various dishes require different utensils.
Variable naming rules require the name to start with a letter or underscore, may contain letters, digits, underscores, or the dollar sign, must not be a Java keyword, and should be meaningful (e.g., name instead of n1).
Values must be compatible with the variable's type; an example shows how to store an employee’s information using appropriate types:
public static void main(String[] args){
String name = "李大爷"; // 姓名
int salary = 8600; // 薪水
char sex = '女'; // 性别
double height = 1.83; // 身高
boolean isDangYuan = false; // 是否党员
}Automatic (widening) type conversion occurs when a smaller type is assigned to a larger type, as demonstrated:
public static void main(String[] args){
char c = '哈'; // 最小
int i = c; // 稍大
float f = i; // 较大
double d = f; // 最大
}Explicit (narrowing) casting is required when assigning a larger type to a smaller one:
public static void main(String[] args){
double d = 86.5;
float f = (float)d;
int i = (int)d;
char c = (char)i;
}Conversions between strings and other types are also covered, showing the most common methods:
public static void main(String[] args){
String s = "10";
int i = Integer.parseInt(s); // string to int
String s1 = String.valueOf(i);
String s2 = Integer.toString(i);
String s3 = "" + i; // int to string via concatenation
}The article concludes by encouraging readers to consult the Java API documentation for deeper details.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
