Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Pure VPN - Up to 27 Months
vgp
Updated:
Friday at 8:10 AM
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
May 30, 2026
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
Testing & Maintenance
Testing Forum
OCA
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="D_Mad" data-source="post: 20585265" data-attributes="member: 476938"><p style="text-align: center"><span style="font-size: 18px"><span style="color: DarkRed">Variable Declarations</span></span></p> <p style="text-align: center"></p> <p style="text-align: center"><span style="font-size: 18px">In our trivial HelloWorld program, we printed a message to the console that cannot be</span></p> <p style="text-align: center"><span style="font-size: 18px">modified by the application user. What if you want to print a greeting to the user? In other</span></p> <p style="text-align: center"><span style="font-size: 18px">words, suppose you want to ask the user her name and print the message “Hello” along with</span></p> <p style="text-align: center"><span style="font-size: 18px">her name. In such situations, you need to accept and store the user’s name somewhere in the</span></p> <p style="text-align: center"><span style="font-size: 18px">program and then later on output it to the console. That’s where you need to have variables in</span></p> <p style="text-align: center"><span style="font-size: 18px">your code. A variable is of a particular data type like primitive data types we discussed earlier,</span></p> <p style="text-align: center"><span style="font-size: 18px">or it may be of a nonprimitive class type, which we will be using in several examples in the</span></p> <p style="text-align: center"><span style="font-size: 18px">book. A variable holds data assigned to it for a certain duration of the application run. In the</span></p> <p style="text-align: center"><span style="font-size: 18px">current situation, you may create a variable called name of the String or Char array data type,</span></p> <p style="text-align: center"><span style="font-size: 18px">ask the user to input her name, and store it in this variable. After the greeting message is</span></p> <p style="text-align: center"><span style="font-size: 18px">printed, the variable may be destroyed. The value assigned to a variable may change on each</span></p> <p style="text-align: center"><span style="font-size: 18px">run or several times within the same run.</span></p> <p style="text-align: center"><span style="font-size: 18px">You need variables for several reasons, such as performing mathematical computations,</span></p> <p style="text-align: center"><span style="font-size: 18px">storing and retrieving data, creating and displaying graphics, and so on. Because you may be</span></p> <p style="text-align: center"><span style="font-size: 18px">creating several variables in a typical application, it is important that you also control their</span></p> <p style="text-align: center"><span style="font-size: 18px">lifetime. Note that each variable occupies a definite storage space in memory, and if you create</span></p> <p style="text-align: center"><span style="font-size: 18px">a variable that lives throughout the life cycle of your application, your program will consume a</span></p> <p style="text-align: center"><span style="font-size: 18px">lot of memory resources during its run. You should create a variable as and only when it is</span></p> <p style="text-align: center"><span style="font-size: 18px">required and destroy it after its use. This will help you conserve precious memory resources.</span></p> <p style="text-align: center"><span style="font-size: 18px">You will learn how to do this in the section on scoping variables later in this syntax reference.</span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: center"><span style="font-size: 18px">Let’s now discuss how to create and use variables in your Java programs. You create variables</span></p> <p style="text-align: center"><span style="font-size: 18px">in Java using the identifiers discussed in the previous section. The following declaration creates a</span></p> <p style="text-align: center"><span style="font-size: 18px">variable called name of type String that can hold string values assigned to it:</span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: center"><span style="font-size: 18px">String name;</span></p> <p style="text-align: center"><span style="font-size: 18px">A variable declaration such as</span></p> <p style="text-align: center"><span style="font-size: 18px">int customerID;</span></p> <p style="text-align: center"><span style="font-size: 18px">holds an integer value.</span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: center"><span style="font-size: 18px">Here are a few more examples of variable declarations and why they are valid or invalid:</span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: center"><span style="font-size: 18px"></span><p style="text-align: left"><span style="font-size: 18px">_sysadmin</span></p> <p style="text-align: left"><span style="font-size: 18px">__myaccount</span></p> <p style="text-align: left"><span style="font-size: 18px">x</span></p> <p style="text-align: left"><span style="font-size: 18px">FFEE</span></p> <p style="text-align: left"><span style="font-size: 18px">Output_sum#amount</span></p> <p style="text-align: left"><span style="font-size: 18px">Output sum#amount</span></p> <p style="text-align: left"><span style="font-size: 18px">1stMember</span></p> <p style="text-align: left"><span style="font-size: 18px">double</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">//</span></p> <p style="text-align: left"><span style="font-size: 18px">a variable name can start with an underscore</span></p> <p style="text-align: left"><span style="font-size: 18px">the underscore character is allowed in any place</span></p> <p style="text-align: left"><span style="font-size: 18px">a variable can consist of a single character</span></p> <p style="text-align: left"><span style="font-size: 18px">a variable can consist of all uppercase alphabets</span></p> <p style="text-align: left"><span style="font-size: 18px">a variable can contain special characters</span></p> <p style="text-align: left"><span style="font-size: 18px">such as a pound sign (#)</span></p> <p style="text-align: left"><span style="font-size: 18px">invalid due to the space character</span></p> <p style="text-align: left"><span style="font-size: 18px">invalid as first letter cannot be a digit</span></p> <p style="text-align: left"><span style="font-size: 18px">invalid as this is a reserved word</span></p> <p style="text-align: left"><span style="font-size: 18px"></span></p> <p style="text-align: left"><span style="font-size: 18px"></span><p style="text-align: center"><span style="font-size: 18px">Tip</span></p> <p style="text-align: center"><span style="font-size: 18px">All identifier declarations in your program code should follow</span></p> <p style="text-align: center"><span style="font-size: 18px">standard naming conventions. Naming conventions make programs</span></p> <p style="text-align: center"><span style="font-size: 18px">easier to read, thus making them more understandable. The</span></p> <p style="text-align: center"><span style="font-size: 18px">convention for declaring variables in Java is that a variable name</span></p> <p style="text-align: center"><span style="font-size: 18px">should use mixed case with a lowercase first alphabet. Internal</span></p> <p style="text-align: center"><span style="font-size: 18px">words start with an uppercase. It is recommended that variable</span></p> <p style="text-align: center"><span style="font-size: 18px">names not start with an underscore and a dollar sign, even though</span></p> <p style="text-align: center"><span style="font-size: 18px">both are allowed characters. The names should be short yet</span></p> <p style="text-align: center"><span style="font-size: 18px">meaningful. One-character names should be avoided except for</span></p> <p style="text-align: center"><span style="font-size: 18px">temporary throwaway variables. Use i, j, k, m, and n for temporary</span></p> <p style="text-align: center"><span style="font-size: 18px">integer declarations; use c, d, and e for characters.</span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: center"><span style="font-size: 18px">Having discussed so much theory, let’s jump into some coding again. The programming example</span></p> <p style="text-align: center"><span style="font-size: 18px">that follows asks the user to input her name and then prints a greeting message containing the name</span></p> <p style="text-align: center"><span style="font-size: 18px">to the console. You will learn how to accept user input in your program code. After this little bit of</span></p> <p style="text-align: center"><span style="font-size: 18px">hands-on practice, we will cover more theory on variable scoping and constants declarations in Java</span></p> <p style="text-align: center"><span style="font-size: 18px">before concluding this syntax reference.</span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> </p> </p></blockquote><p></p>
[QUOTE="D_Mad, post: 20585265, member: 476938"] [CENTER][SIZE=5][COLOR=DarkRed]Variable Declarations[/COLOR][/SIZE] [SIZE=5]In our trivial HelloWorld program, we printed a message to the console that cannot be modified by the application user. What if you want to print a greeting to the user? In other words, suppose you want to ask the user her name and print the message “Hello” along with her name. In such situations, you need to accept and store the user’s name somewhere in the program and then later on output it to the console. That’s where you need to have variables in your code. A variable is of a particular data type like primitive data types we discussed earlier, or it may be of a nonprimitive class type, which we will be using in several examples in the book. A variable holds data assigned to it for a certain duration of the application run. In the current situation, you may create a variable called name of the String or Char array data type, ask the user to input her name, and store it in this variable. After the greeting message is printed, the variable may be destroyed. The value assigned to a variable may change on each run or several times within the same run. You need variables for several reasons, such as performing mathematical computations, storing and retrieving data, creating and displaying graphics, and so on. Because you may be creating several variables in a typical application, it is important that you also control their lifetime. Note that each variable occupies a definite storage space in memory, and if you create a variable that lives throughout the life cycle of your application, your program will consume a lot of memory resources during its run. You should create a variable as and only when it is required and destroy it after its use. This will help you conserve precious memory resources. You will learn how to do this in the section on scoping variables later in this syntax reference. Let’s now discuss how to create and use variables in your Java programs. You create variables in Java using the identifiers discussed in the previous section. The following declaration creates a variable called name of type String that can hold string values assigned to it: String name; A variable declaration such as int customerID; holds an integer value. Here are a few more examples of variable declarations and why they are valid or invalid: [/SIZE][LEFT][SIZE=5]_sysadmin __myaccount x FFEE Output_sum#amount Output sum#amount 1stMember double // // // // // // // // // a variable name can start with an underscore the underscore character is allowed in any place a variable can consist of a single character a variable can consist of all uppercase alphabets a variable can contain special characters such as a pound sign (#) invalid due to the space character invalid as first letter cannot be a digit invalid as this is a reserved word [/SIZE][CENTER][SIZE=5]Tip All identifier declarations in your program code should follow standard naming conventions. Naming conventions make programs easier to read, thus making them more understandable. The convention for declaring variables in Java is that a variable name should use mixed case with a lowercase first alphabet. Internal words start with an uppercase. It is recommended that variable names not start with an underscore and a dollar sign, even though both are allowed characters. The names should be short yet meaningful. One-character names should be avoided except for temporary throwaway variables. Use i, j, k, m, and n for temporary integer declarations; use c, d, and e for characters. Having discussed so much theory, let’s jump into some coding again. The programming example that follows asks the user to input her name and then prints a greeting message containing the name to the console. You will learn how to accept user input in your program code. After this little bit of hands-on practice, we will cover more theory on variable scoping and constants declarations in Java before concluding this syntax reference. [/SIZE][/CENTER] [/LEFT] [/CENTER] [/QUOTE]
Insert quotes…
Verification
Hath warak paha keeyada? (hatha wadikireema paha)
Post reply
Top
Bottom