Explore the English language on a new scale using AI-powered English language navigator.
Variables in JavaAll programs, except the simplest ones, require storing some data during running time. It could be input from user, data from file, result of calculations inside the program and so on and so forth. Variables provide mechanism to store data inside a program. A variable has
Below we show several examples defining variables:
Variable's value could be changed using the assignment operator: a = 5; Note. Before you are allowed to use a variable it must be initialized. Otherwise program won't compile: Codepublic class Variables { public static void main(String[] args) { int byteVar; // byteVar isn't initialized System.out.println("byte variable's value: " + byteVar); } } OutputException in thread "main" java.lang.Error: Unresolved compilation problem: The local variable byteVar may not have been initialized
at Variables.main(Variables.java:5) Which names are allowedVariable names in Java are case-sensitive. Variable name:
Above we showed the names, which are correct from the viewpoint of Java language's syntax. But Java also has naming conventions and we strongly recommend you to name your variables according to them. There are simple rules of giving good names. Variables's name:
ExampleVariables.javapublic class Variables { public static void main(String[] args) { int a = 5, b, c; b = a + 5; c = a * b; System.out.println("a: " + a); System.out.println("b: " + b); System.out.println("c: " + c); } } Outputa: 5
Partners Ads
|