<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://ideawaza.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=70.17.19.237</id>
	<title>IdeaWazaWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://ideawaza.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=70.17.19.237"/>
	<link rel="alternate" type="text/html" href="https://ideawaza.com/wiki/Special:Contributions/70.17.19.237"/>
	<updated>2026-09-30T23:48:56Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://ideawaza.com/index.php?title=Basic_Java_Language&amp;diff=36759</id>
		<title>Basic Java Language</title>
		<link rel="alternate" type="text/html" href="https://ideawaza.com/index.php?title=Basic_Java_Language&amp;diff=36759"/>
		<updated>2007-01-13T14:08:27Z</updated>

		<summary type="html">&lt;p&gt;70.17.19.237: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This lesson discusses the basics of the Java language, including variables, primitives, operators, statements and Java writing conventions.&lt;br /&gt;
&lt;br /&gt;
=Touch on Classes=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Everything in Java is done inside classes. Classes are bodies of statements, methods, and variables. A statement can be thought of as the smallest standalone element of an imperative programming language (or a language that explicitly specify an algorithm to achieve a goal, opposed to declarative programming, which explicitly specifies a goal and leaves the implementation of that goal to algorithms within the support software). A program is formed by a sequence of one or more statements (e.g. if x, then y). A statement will have internal components (e.g. expressions). A method usually consists of a sequence of statements to perform an action, a set of input parameters to customize those actions, and possibly an output value (called return value) of some kind. There is a method called main that all classes have. This is the start point of your program - where everything must begin. To create a class, the syntax (or basic outline in code) is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[Protection] class [Name] [Extend/Implements...]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Right now, worry about the first 3. &amp;quot;[Protection]&amp;quot; refers to permissions of who can access the class, and will be discussed later. The text of &amp;quot;class&amp;quot; is required to create a class. &amp;quot;Name&amp;quot; can be any valid identifier (see Valid Identifiers...). The name will be used to create objects (more later...). Here is what you type to create a class, as for now, and all you must worry about:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NAME&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
NAME, again,is the identifier.  (Java is case sensitive; so NAME is different from Name.  Industry convention suggest naming classes using mixed case, starting with an uppercase letter.[http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html]  For example:  NameNumberTwo)&lt;br /&gt;
&lt;br /&gt;
Classes also have a body - where you actually put the things. To create the body you have a { and }. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NAME&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This, essentially creates a class. All the code goes between { and }.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Protection=&lt;br /&gt;
{{Template:confusing|December 2006}}&lt;br /&gt;
Java allows you to create a &amp;quot;protection value&amp;quot;. This means you can set who (or what - your program, other people&#039;s programs, etc.) can access the variable, method, or class that you set to a protection. You have already seen where this goes in a class, and will learn where in variables and methods. Right now, just call everything public. Here is a chart (which you might not understand right now).&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| &#039;&#039;&#039;Name&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Desc.&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Example + Meaning&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
| private&lt;br /&gt;
| Variable only accessible from class it is in&lt;br /&gt;
| private int myInt;//myInt can only be edited and viewed from the class it is in&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| (No modifier) - only accessible from class and subclass&lt;br /&gt;
| int myInt;//A subclass can access it, as the class it is in&lt;br /&gt;
|-&lt;br /&gt;
| protected&lt;br /&gt;
| Accessible from package, subclass, and class&lt;br /&gt;
| protected int myInt;//Class, subclass, and package classes can access it&lt;br /&gt;
|-&lt;br /&gt;
| public&lt;br /&gt;
| Fully accessible&lt;br /&gt;
| public int myInt;//Any class can access it&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;quot;class it is in&amp;quot; means the class that you have created using &amp;lt;code&amp;gt;public class NAME&amp;lt;/code&amp;gt;. You will learn about packages later, but essentially they are a package of related code (classes). &amp;quot;Subclass&amp;quot; is that it can be accessed from any class that extends it (see Inheritance).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Protection of classes is a little different. When you say &amp;quot;public&amp;quot; it means that the class is the main class in that .java file. Notice &amp;quot;main class&amp;quot; - you can have multiple classes in the same .java file, but only one public or &amp;quot;main&amp;quot; class.&lt;br /&gt;
&lt;br /&gt;
=Statements=&lt;br /&gt;
All calculations are done in statements. Every statement ends in a semicolon: ;. Statements can set a variable with the &#039;=&#039; operator. This sets the variable you wish to set to another variable or a value. Values can only be used on primitive types.  As you saw in the HelloWorld program, there was one statement: &amp;lt;code&amp;gt;System.out.println(&amp;quot;Hello, World&amp;quot;)&amp;lt;/code&amp;gt;. This is one type of statement called calling a method. More will be discussed later. Finally, you can declare a variable. If a class has no statements, it would do nothing.&lt;br /&gt;
&lt;br /&gt;
=Variables=&lt;br /&gt;
Variables Variables are used to store data into your computer memory, that can change. All you need to worry about is that you can create a variable and change its value. There are two main categories of variables: reference variables and primitive variables. Reference variables are place holders for objects (More on objects in Lesson 4, Java Objects and Classes.) Primitive variables are given a value of a primitive, a basic value. Variables are all given a type when they are declared, such as &amp;quot;int&amp;quot; or &amp;quot;String&amp;quot;.&lt;br /&gt;
==Declaring Variables==&lt;br /&gt;
To declare a variable, use this syntax:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[protector] [classname] [identifier]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is one type of statement. The class name is the type of variable you want. Currently, you will only use basic primitive variables, but you will learn about objects soon. Here is an example of declaring a variable:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public int myInt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
myInt has protection of public. It is of class int, or Integer (see Primitive Types). int is a class in Java. When you say that, you are basically making a primitive object.&lt;br /&gt;
==Primitive Data Types==&lt;br /&gt;
Primitives are the simplest type of data. Primitives consist of numbers, letters and strings of letters (more than one letter in one variable). The following table shows all the types of primitives.&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| &#039;&#039;&#039;Primitive&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Description&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Values&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
| byte&lt;br /&gt;
| A brief, 8-bit integer&lt;br /&gt;
| Whole numbers -128 through 127&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| A short, 16-bit integer&lt;br /&gt;
| Whole numbers -32,768 through 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int&lt;br /&gt;
| A 32 bit integer&lt;br /&gt;
| Whole numbers -2,147,483,648 through 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| long&lt;br /&gt;
| A long, 64 bit integer&lt;br /&gt;
| Whole numbers -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| float&lt;br /&gt;
| Single-precision floating point (32-bit IEEE 754)&lt;br /&gt;
| Smallest positive non-zero: 14e&amp;lt;sup&amp;gt;-45&amp;lt;/sup&amp;gt;, Largest positive non-zero: 3.4028234e&amp;lt;sup&amp;gt;38&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| double&lt;br /&gt;
| Double-precision floating point (64-bit IEEE 754)&lt;br /&gt;
| Smallest positive non-zero: 4.9e&amp;lt;sup&amp;gt;-324&amp;lt;/sup&amp;gt;, Largest positive non-zero: 1.797693157e&amp;lt;sup&amp;gt;308&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| A single character&lt;br /&gt;
| All Unicode characters&lt;br /&gt;
|-&lt;br /&gt;
| boolean&lt;br /&gt;
| A Boolean value&lt;br /&gt;
| &#039;&#039;&#039;true&#039;&#039;&#039; or &#039;&#039;&#039;false&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| A group of characters&lt;br /&gt;
| Any formation of characters&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using what you have learned, I am sure you can guess how to create a long or double. &amp;lt;code&amp;gt;long myLong;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;double myDouble;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Operators==&lt;br /&gt;
&lt;br /&gt;
===Assignment Operator &#039;=&#039;===&lt;br /&gt;
The assignment operator sets the variable on its left equal to the value on its right. This code creates the variable &#039;&#039;a&#039;&#039; and sets it equal to 5&lt;br /&gt;
&amp;lt;pre&amp;gt;int a;&lt;br /&gt;
a = 5;&amp;lt;/pre&amp;gt;&lt;br /&gt;
Fairly simple, right? Here is where it can get a little more tricky. &lt;br /&gt;
&amp;lt;pre&amp;gt;int a;&lt;br /&gt;
int b;&lt;br /&gt;
b = 6;&lt;br /&gt;
a = b; //a equals 6&amp;lt;/pre&amp;gt;&lt;br /&gt;
At first glance, you might think that the last statement is setting &#039;&#039;a&#039;&#039; equal to the letter &amp;quot;b&amp;quot;, but instead it is setting &#039;&#039;a&#039;&#039; equal to the &#039;&#039;value&#039;&#039; of &#039;&#039;b&#039;&#039;, which is 6. Essentially, a &#039;&#039;&#039;is&#039;&#039;&#039; now 6. You can also set a variable and declare it on the same line:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int a = 6;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, you can&#039;t say &amp;lt;code&amp;gt;char myChar = y;&amp;lt;/code&amp;gt; to set a character to &#039;y&#039;. You have to use &#039; &#039; for characters. That is: &amp;lt;code&amp;gt;char myChar = &#039;y&#039;;&amp;lt;/code&amp;gt;. For Strings, use &amp;quot; &amp;quot;. That is: &amp;lt;code&amp;gt;String myString = &amp;quot;This is an example of a string&amp;quot;&amp;lt;/code&amp;gt;. Note how characters are just 1 and strings can consist of entire words.&lt;br /&gt;
&lt;br /&gt;
Finally, what is the difference between double/float and int/long. Well, doubles and floats can have decimals: &amp;lt;code&amp;gt;float myFloat = 5.1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, why would you want to set a variable? For further use, ie, to access at a later time. For example, you can do calculations with other operators...&lt;br /&gt;
&lt;br /&gt;
===Addition Operator +===&lt;br /&gt;
The addition operator is different from the = operator. It does not set anything. You would need the = operator to do any setting.&lt;br /&gt;
&lt;br /&gt;
If you want to you want to use the addition operator to add 6 to the value of &#039;&#039;a&#039;&#039;, it would be used like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;a = a + 6;&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can use a shortcut in order to reach the same result as the above.&lt;br /&gt;
&amp;lt;pre&amp;gt;a += 6;&amp;lt;/pre&amp;gt;&lt;br /&gt;
There are shortcuts to all of the operators, which will be shown in the following sections.&lt;br /&gt;
&lt;br /&gt;
===Subtraction -, Division / and Multiplication * Operators===&lt;br /&gt;
&lt;br /&gt;
Just like the addition operator, the Subtraction, Division, and Multiplication operators are used as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int a;&lt;br /&gt;
&lt;br /&gt;
a = a-1;&lt;br /&gt;
a = a/1;&lt;br /&gt;
a = a*1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result of &amp;quot;a&amp;quot; in each example would be simply&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1st a - &amp;quot;a&amp;quot; minus 1&lt;br /&gt;
2nd a - &amp;quot;a&amp;quot; divided by 1&lt;br /&gt;
3rd a - &amp;quot;a&amp;quot; multiplied by 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When all of the operators are used at once, for example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int a, b, c, d;&lt;br /&gt;
&lt;br /&gt;
a = a-b/c*d;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Java Programming Language would use the order of operations in this example.&lt;br /&gt;
&lt;br /&gt;
Here is a shortcut for subtracting a value from int myInt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myInt -= 4;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=Valid Identifiers=&lt;br /&gt;
There are several rules for the naming of your variables. Identifiers can be named using all the letters from A through Z (capital or lowercase, you choose), numbers 0-9 and underscore _ and the dollar sign $. The first character of the identifier cannot be a number, but it can be a letter, _ or $. &lt;br /&gt;
&lt;br /&gt;
You can&#039;t create two variables with the same identifier, all identifiers must be unique. You also can&#039;t use an identifier that is a Java keyword. Here is a list of all the [http://en.wikipedia.org/wiki/Java_keywords Java Keywords].  [[Flashcards for Java keywords]] provides some assistance in memorizing the concepts behind specific keywords.&lt;br /&gt;
=Using the Displayer with Variables=&lt;br /&gt;
This section is about printing to the console using &amp;lt;code&amp;gt;System.out.println();&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For Strings, it is extremely simple:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(STRING IDENTIFIER HERE);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
But for other variables, you have to do it like this:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(VARIABLE IDENT + &amp;quot;&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is because that method&#039;s parameter requires a string, and to change a variable to a string you say +&amp;quot;&amp;quot;. Note that this only works on primitive types. On objects you have to have a method toString(). (You will read more).&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(54 + &amp;quot;&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also do multiple variables on the same line:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(false + 54 + &amp;quot;&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can put it anywhere: &amp;lt;code&amp;gt;System.out.println(false + &amp;quot;&amp;quot; + 54)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that this won&#039;t work for two number values:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(54 + 32)&amp;lt;/code&amp;gt;. It would mistake it for the adding operator for values. For strings, the operator is different. So you would have to do: &amp;lt;code&amp;gt;System.out.println(54+&amp;quot;&amp;quot;+32)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now you can do calculations and print the result! For example, say you want to print 6*6*6*6. Here is the code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Multiplying&lt;br /&gt;
{&lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
    int a = 6;&lt;br /&gt;
    int b = a*a*a*a;&lt;br /&gt;
    System.out.println(b + &amp;quot;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comments= 	 &lt;br /&gt;
Comments are notes that the developer can insert into their code. This allows the developer to keep track of their code and helps when developers are collaborating on a project. It is good to write a summary of the following code at the beginning of your methods, classes, etc. The Java compiler completely ignores comments, so you can say anything in them 	 &lt;br /&gt;
 		 &lt;br /&gt;
Comments are written using two forward slashes 	 &lt;br /&gt;
&amp;lt;pre&amp;gt;//This is a comment&amp;lt;/pre&amp;gt; 	 &lt;br /&gt;
Comments can be written at the end of a line 	 &lt;br /&gt;
&amp;lt;pre&amp;gt;System.out.println(&amp;quot;I love comments&amp;quot;); //This is also a comment&amp;lt;/pre&amp;gt; 	 &lt;br /&gt;
Comments can also be written on multiple lines by starting with &#039;&#039;&#039;/*&#039;&#039;&#039; and ending with&#039;&#039;&#039;*/&#039;&#039;&#039;. 	 &lt;br /&gt;
&amp;lt;pre&amp;gt;/* Comment 1 	 &lt;br /&gt;
Comment 2 	 &lt;br /&gt;
Comment 3 */&amp;lt;/pre&amp;gt; 	 &lt;br /&gt;
Sun recommends the above form be reserved for information intended for automatic JavaDoc documentation.&lt;br /&gt;
&lt;br /&gt;
=Compiling and Running your program=&lt;br /&gt;
To compile your program, go to the directory you saved it. Note that if your class was named NaMe then your file &#039;&#039;&#039;must&#039;&#039;&#039; be named NaMe.java. Anyways, type: &amp;lt;code&amp;gt;javac NaMe.java&amp;lt;/code&amp;gt; where NaMe is your class name. To run, type &amp;lt;code&amp;gt;java NaMe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that if the compiler gives errors with your code, you must fix them.&lt;br /&gt;
&lt;br /&gt;
If, when running, it says invalid class file, perhaps the compiler did not create a file. It might have given error messages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Asking the User=&lt;br /&gt;
Now, let&#039;s go on to asking the user to enter a number and doing calculations on it. Basically: &amp;lt;code&amp;gt;JOptionPane.showInputDialog(null, &amp;quot;Hello. Enter something&amp;quot;)&amp;lt;/code&amp;gt;. Try putting that in a class (in the main method, of course) and run it. Currently, we can&#039;t do anything with it because the value is deleted.&lt;br /&gt;
&lt;br /&gt;
Because what you enter can consist of letters OR numbers, and any length of them, what is &amp;quot;produced&amp;quot; by that is a String. The following will work: &amp;lt;code&amp;gt;String mine = JOptionPane.showInputDialog(null, &amp;quot;Now I can set a String!&amp;quot;);&amp;lt;/code&amp;gt;. Now lets say that after you get the string you want to print it. Just like regularly, add the following line: &amp;lt;code&amp;gt;System.out.println(mine);&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now you probably want to be able to enter a number. Java provides a method of changing a String to an int. It is Integer.parseInt(String_Here);. So lets say you want the user to enter a number and you print it. Do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int mine = Integer.parseInt(JOptionPane.showInputDialog(null, &amp;quot;Now I can set an INTEGER!&amp;quot;));&lt;br /&gt;
System.out.println(mine);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And now, of course, you can do whatever you want with that...&lt;br /&gt;
&lt;br /&gt;
=Exercises=&lt;br /&gt;
A. Create a program that creates a String with the value &amp;quot;ABC&amp;quot;. Then print the value using System.out.println(StringIdentifierHere). You could also do System.out.prinln(&amp;quot;ABC&amp;quot;), but we are focusing on creating variables.&lt;br /&gt;
&lt;br /&gt;
B. Do the above with a long set to 10000. Note that you have to say System.out.println(LongIdentifier + &amp;quot;&amp;quot;).&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;hr&amp;gt;&lt;br /&gt;
&amp;lt;!--[[Topic:Java|Back to Java Main Page]] | [[Introduction_to_Java|Previous Lesson]] | [[Java_Decision_Structures|Next Lesson]]--&amp;gt;&lt;br /&gt;
{{1lvlnav|[[Topic:Java|Java]]|[[Introduction to Java]]|[[Java Decision Structures]]}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Java]]&lt;br /&gt;
[[Category:Learning activities]]&lt;/div&gt;</summary>
		<author><name>70.17.19.237</name></author>
	</entry>
	<entry>
		<id>https://ideawaza.com/index.php?title=Basic_Java_Language&amp;diff=36758</id>
		<title>Basic Java Language</title>
		<link rel="alternate" type="text/html" href="https://ideawaza.com/index.php?title=Basic_Java_Language&amp;diff=36758"/>
		<updated>2007-01-13T14:06:12Z</updated>

		<summary type="html">&lt;p&gt;70.17.19.237: /* Touch on Classes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This lesson discusses the basics of the Java language, including variables, primitives, operators, statements and Java writing conventions.&lt;br /&gt;
&lt;br /&gt;
=Touch on Classes=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Everything in Java is done inside classes. Classes are bodies of statements, methods, and variables. A statement can be thought of as the smallest standalone element of an imperative programming language (or a language that explicitly specify an algorithm to achieve a goal, opposed to declarative programming, which explicitly specifies a goal and leaves the implementation of that goal to algorithms within the support software). A program is formed by a sequence of one or more statements (e.g. if x, then y). A statement will have internal components (e.g. expressions). A method usually consists of a sequence of statements to perform an action, a set of input parameters to customize those actions, and possibly an output value (called return value) of some kind. There is a method called main that all classes have. This is the start point of your program - where everything must begin. To create a class, the syntax (or basic outline in code) is:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[Protection] class [Name] [Extend/Implements...]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Right now, worry about the first 3. &amp;quot;[Protection]&amp;quot; refers to permissions of who can access the class, and will be discussed later. The text of &amp;quot;class&amp;quot; is required to create a class. &amp;quot;Name&amp;quot; can be any valid identifier (see Valid Identifiers...). The name will be used to create objects (more later...). Here is what you type to create a class, as for now, and all you must worry about:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NAME&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
NAME, again,is the identifier.  (Java is case sensitive; so NAME is different from Name.  Industry convention suggest naming classes using mixed case, starting with an uppercase letter.[http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html]  For example:  NameNumberTwo)&lt;br /&gt;
&lt;br /&gt;
Classes also have a body - where you actually put the things. To create the body you have a { and }. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NAME&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This, essentially creates a class. All the code goes between { and }.&lt;br /&gt;
&lt;br /&gt;
==a subsection ==&lt;br /&gt;
This is some sample text in my subsection.&lt;br /&gt;
&lt;br /&gt;
=Protection=&lt;br /&gt;
{{Template:confusing|December 2006}}&lt;br /&gt;
Java allows you to create a &amp;quot;protection value&amp;quot;. This means you can set who (or what - your program, other people&#039;s programs, etc.) can access the variable, method, or class that you set to a protection. You have already seen where this goes in a class, and will learn where in variables and methods. Right now, just call everything public. Here is a chart (which you might not understand right now).&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| &#039;&#039;&#039;Name&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Desc.&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Example + Meaning&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
| private&lt;br /&gt;
| Variable only accessible from class it is in&lt;br /&gt;
| private int myInt;//myInt can only be edited and viewed from the class it is in&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| (No modifier) - only accessible from class and subclass&lt;br /&gt;
| int myInt;//A subclass can access it, as the class it is in&lt;br /&gt;
|-&lt;br /&gt;
| protected&lt;br /&gt;
| Accessible from package, subclass, and class&lt;br /&gt;
| protected int myInt;//Class, subclass, and package classes can access it&lt;br /&gt;
|-&lt;br /&gt;
| public&lt;br /&gt;
| Fully accessible&lt;br /&gt;
| public int myInt;//Any class can access it&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;quot;class it is in&amp;quot; means the class that you have created using &amp;lt;code&amp;gt;public class NAME&amp;lt;/code&amp;gt;. You will learn about packages later, but essentially they are a package of related code (classes). &amp;quot;Subclass&amp;quot; is that it can be accessed from any class that extends it (see Inheritance).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Protection of classes is a little different. When you say &amp;quot;public&amp;quot; it means that the class is the main class in that .java file. Notice &amp;quot;main class&amp;quot; - you can have multiple classes in the same .java file, but only one public or &amp;quot;main&amp;quot; class.&lt;br /&gt;
&lt;br /&gt;
=Statements=&lt;br /&gt;
All calculations are done in statements. Every statement ends in a semicolon: ;. Statements can set a variable with the &#039;=&#039; operator. This sets the variable you wish to set to another variable or a value. Values can only be used on primitive types.  As you saw in the HelloWorld program, there was one statement: &amp;lt;code&amp;gt;System.out.println(&amp;quot;Hello, World&amp;quot;)&amp;lt;/code&amp;gt;. This is one type of statement called calling a method. More will be discussed later. Finally, you can declare a variable. If a class has no statements, it would do nothing.&lt;br /&gt;
&lt;br /&gt;
=Variables=&lt;br /&gt;
Variables Variables are used to store data into your computer memory, that can change. All you need to worry about is that you can create a variable and change its value. There are two main categories of variables: reference variables and primitive variables. Reference variables are place holders for objects (More on objects in Lesson 4, Java Objects and Classes.) Primitive variables are given a value of a primitive, a basic value. Variables are all given a type when they are declared, such as &amp;quot;int&amp;quot; or &amp;quot;String&amp;quot;.&lt;br /&gt;
==Declaring Variables==&lt;br /&gt;
To declare a variable, use this syntax:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[protector] [classname] [identifier]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is one type of statement. The class name is the type of variable you want. Currently, you will only use basic primitive variables, but you will learn about objects soon. Here is an example of declaring a variable:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public int myInt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
myInt has protection of public. It is of class int, or Integer (see Primitive Types). int is a class in Java. When you say that, you are basically making a primitive object.&lt;br /&gt;
==Primitive Data Types==&lt;br /&gt;
Primitives are the simplest type of data. Primitives consist of numbers, letters and strings of letters (more than one letter in one variable). The following table shows all the types of primitives.&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| border=1 cellspacing=0 cellpadding=5&lt;br /&gt;
| &#039;&#039;&#039;Primitive&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Description&#039;&#039;&#039;&lt;br /&gt;
| &#039;&#039;&#039;Values&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
| byte&lt;br /&gt;
| A brief, 8-bit integer&lt;br /&gt;
| Whole numbers -128 through 127&lt;br /&gt;
|-&lt;br /&gt;
| short&lt;br /&gt;
| A short, 16-bit integer&lt;br /&gt;
| Whole numbers -32,768 through 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int&lt;br /&gt;
| A 32 bit integer&lt;br /&gt;
| Whole numbers -2,147,483,648 through 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| long&lt;br /&gt;
| A long, 64 bit integer&lt;br /&gt;
| Whole numbers -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| float&lt;br /&gt;
| Single-precision floating point (32-bit IEEE 754)&lt;br /&gt;
| Smallest positive non-zero: 14e&amp;lt;sup&amp;gt;-45&amp;lt;/sup&amp;gt;, Largest positive non-zero: 3.4028234e&amp;lt;sup&amp;gt;38&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| double&lt;br /&gt;
| Double-precision floating point (64-bit IEEE 754)&lt;br /&gt;
| Smallest positive non-zero: 4.9e&amp;lt;sup&amp;gt;-324&amp;lt;/sup&amp;gt;, Largest positive non-zero: 1.797693157e&amp;lt;sup&amp;gt;308&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| char&lt;br /&gt;
| A single character&lt;br /&gt;
| All Unicode characters&lt;br /&gt;
|-&lt;br /&gt;
| boolean&lt;br /&gt;
| A Boolean value&lt;br /&gt;
| &#039;&#039;&#039;true&#039;&#039;&#039; or &#039;&#039;&#039;false&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| A group of characters&lt;br /&gt;
| Any formation of characters&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using what you have learned, I am sure you can guess how to create a long or double. &amp;lt;code&amp;gt;long myLong;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;double myDouble;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Operators==&lt;br /&gt;
&lt;br /&gt;
===Assignment Operator &#039;=&#039;===&lt;br /&gt;
The assignment operator sets the variable on its left equal to the value on its right. This code creates the variable &#039;&#039;a&#039;&#039; and sets it equal to 5&lt;br /&gt;
&amp;lt;pre&amp;gt;int a;&lt;br /&gt;
a = 5;&amp;lt;/pre&amp;gt;&lt;br /&gt;
Fairly simple, right? Here is where it can get a little more tricky. &lt;br /&gt;
&amp;lt;pre&amp;gt;int a;&lt;br /&gt;
int b;&lt;br /&gt;
b = 6;&lt;br /&gt;
a = b; //a equals 6&amp;lt;/pre&amp;gt;&lt;br /&gt;
At first glance, you might think that the last statement is setting &#039;&#039;a&#039;&#039; equal to the letter &amp;quot;b&amp;quot;, but instead it is setting &#039;&#039;a&#039;&#039; equal to the &#039;&#039;value&#039;&#039; of &#039;&#039;b&#039;&#039;, which is 6. Essentially, a &#039;&#039;&#039;is&#039;&#039;&#039; now 6. You can also set a variable and declare it on the same line:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int a = 6;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, you can&#039;t say &amp;lt;code&amp;gt;char myChar = y;&amp;lt;/code&amp;gt; to set a character to &#039;y&#039;. You have to use &#039; &#039; for characters. That is: &amp;lt;code&amp;gt;char myChar = &#039;y&#039;;&amp;lt;/code&amp;gt;. For Strings, use &amp;quot; &amp;quot;. That is: &amp;lt;code&amp;gt;String myString = &amp;quot;This is an example of a string&amp;quot;&amp;lt;/code&amp;gt;. Note how characters are just 1 and strings can consist of entire words.&lt;br /&gt;
&lt;br /&gt;
Finally, what is the difference between double/float and int/long. Well, doubles and floats can have decimals: &amp;lt;code&amp;gt;float myFloat = 5.1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, why would you want to set a variable? For further use, ie, to access at a later time. For example, you can do calculations with other operators...&lt;br /&gt;
&lt;br /&gt;
===Addition Operator +===&lt;br /&gt;
The addition operator is different from the = operator. It does not set anything. You would need the = operator to do any setting.&lt;br /&gt;
&lt;br /&gt;
If you want to you want to use the addition operator to add 6 to the value of &#039;&#039;a&#039;&#039;, it would be used like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;a = a + 6;&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can use a shortcut in order to reach the same result as the above.&lt;br /&gt;
&amp;lt;pre&amp;gt;a += 6;&amp;lt;/pre&amp;gt;&lt;br /&gt;
There are shortcuts to all of the operators, which will be shown in the following sections.&lt;br /&gt;
&lt;br /&gt;
===Subtraction -, Division / and Multiplication * Operators===&lt;br /&gt;
&lt;br /&gt;
Just like the addition operator, the Subtraction, Division, and Multiplication operators are used as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int a;&lt;br /&gt;
&lt;br /&gt;
a = a-1;&lt;br /&gt;
a = a/1;&lt;br /&gt;
a = a*1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result of &amp;quot;a&amp;quot; in each example would be simply&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1st a - &amp;quot;a&amp;quot; minus 1&lt;br /&gt;
2nd a - &amp;quot;a&amp;quot; divided by 1&lt;br /&gt;
3rd a - &amp;quot;a&amp;quot; multiplied by 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When all of the operators are used at once, for example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int a, b, c, d;&lt;br /&gt;
&lt;br /&gt;
a = a-b/c*d;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Java Programming Language would use the order of operations in this example.&lt;br /&gt;
&lt;br /&gt;
Here is a shortcut for subtracting a value from int myInt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myInt -= 4;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=Valid Identifiers=&lt;br /&gt;
There are several rules for the naming of your variables. Identifiers can be named using all the letters from A through Z (capital or lowercase, you choose), numbers 0-9 and underscore _ and the dollar sign $. The first character of the identifier cannot be a number, but it can be a letter, _ or $. &lt;br /&gt;
&lt;br /&gt;
You can&#039;t create two variables with the same identifier, all identifiers must be unique. You also can&#039;t use an identifier that is a Java keyword. Here is a list of all the [http://en.wikipedia.org/wiki/Java_keywords Java Keywords].  [[Flashcards for Java keywords]] provides some assistance in memorizing the concepts behind specific keywords.&lt;br /&gt;
=Using the Displayer with Variables=&lt;br /&gt;
This section is about printing to the console using &amp;lt;code&amp;gt;System.out.println();&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For Strings, it is extremely simple:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(STRING IDENTIFIER HERE);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
But for other variables, you have to do it like this:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(VARIABLE IDENT + &amp;quot;&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is because that method&#039;s parameter requires a string, and to change a variable to a string you say +&amp;quot;&amp;quot;. Note that this only works on primitive types. On objects you have to have a method toString(). (You will read more).&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(54 + &amp;quot;&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also do multiple variables on the same line:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(false + 54 + &amp;quot;&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can put it anywhere: &amp;lt;code&amp;gt;System.out.println(false + &amp;quot;&amp;quot; + 54)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that this won&#039;t work for two number values:&lt;br /&gt;
&amp;lt;code&amp;gt;System.out.println(54 + 32)&amp;lt;/code&amp;gt;. It would mistake it for the adding operator for values. For strings, the operator is different. So you would have to do: &amp;lt;code&amp;gt;System.out.println(54+&amp;quot;&amp;quot;+32)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now you can do calculations and print the result! For example, say you want to print 6*6*6*6. Here is the code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Multiplying&lt;br /&gt;
{&lt;br /&gt;
  public static void main(String args[])&lt;br /&gt;
  {&lt;br /&gt;
    int a = 6;&lt;br /&gt;
    int b = a*a*a*a;&lt;br /&gt;
    System.out.println(b + &amp;quot;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comments= 	 &lt;br /&gt;
Comments are notes that the developer can insert into their code. This allows the developer to keep track of their code and helps when developers are collaborating on a project. It is good to write a summary of the following code at the beginning of your methods, classes, etc. The Java compiler completely ignores comments, so you can say anything in them 	 &lt;br /&gt;
 		 &lt;br /&gt;
Comments are written using two forward slashes 	 &lt;br /&gt;
&amp;lt;pre&amp;gt;//This is a comment&amp;lt;/pre&amp;gt; 	 &lt;br /&gt;
Comments can be written at the end of a line 	 &lt;br /&gt;
&amp;lt;pre&amp;gt;System.out.println(&amp;quot;I love comments&amp;quot;); //This is also a comment&amp;lt;/pre&amp;gt; 	 &lt;br /&gt;
Comments can also be written on multiple lines by starting with &#039;&#039;&#039;/*&#039;&#039;&#039; and ending with&#039;&#039;&#039;*/&#039;&#039;&#039;. 	 &lt;br /&gt;
&amp;lt;pre&amp;gt;/* Comment 1 	 &lt;br /&gt;
Comment 2 	 &lt;br /&gt;
Comment 3 */&amp;lt;/pre&amp;gt; 	 &lt;br /&gt;
Sun recommends the above form be reserved for information intended for automatic JavaDoc documentation.&lt;br /&gt;
&lt;br /&gt;
=Compiling and Running your program=&lt;br /&gt;
To compile your program, go to the directory you saved it. Note that if your class was named NaMe then your file &#039;&#039;&#039;must&#039;&#039;&#039; be named NaMe.java. Anyways, type: &amp;lt;code&amp;gt;javac NaMe.java&amp;lt;/code&amp;gt; where NaMe is your class name. To run, type &amp;lt;code&amp;gt;java NaMe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that if the compiler gives errors with your code, you must fix them.&lt;br /&gt;
&lt;br /&gt;
If, when running, it says invalid class file, perhaps the compiler did not create a file. It might have given error messages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Asking the User=&lt;br /&gt;
Now, let&#039;s go on to asking the user to enter a number and doing calculations on it. Basically: &amp;lt;code&amp;gt;JOptionPane.showInputDialog(null, &amp;quot;Hello. Enter something&amp;quot;)&amp;lt;/code&amp;gt;. Try putting that in a class (in the main method, of course) and run it. Currently, we can&#039;t do anything with it because the value is deleted.&lt;br /&gt;
&lt;br /&gt;
Because what you enter can consist of letters OR numbers, and any length of them, what is &amp;quot;produced&amp;quot; by that is a String. The following will work: &amp;lt;code&amp;gt;String mine = JOptionPane.showInputDialog(null, &amp;quot;Now I can set a String!&amp;quot;);&amp;lt;/code&amp;gt;. Now lets say that after you get the string you want to print it. Just like regularly, add the following line: &amp;lt;code&amp;gt;System.out.println(mine);&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now you probably want to be able to enter a number. Java provides a method of changing a String to an int. It is Integer.parseInt(String_Here);. So lets say you want the user to enter a number and you print it. Do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int mine = Integer.parseInt(JOptionPane.showInputDialog(null, &amp;quot;Now I can set an INTEGER!&amp;quot;));&lt;br /&gt;
System.out.println(mine);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And now, of course, you can do whatever you want with that...&lt;br /&gt;
&lt;br /&gt;
=Exercises=&lt;br /&gt;
A. Create a program that creates a String with the value &amp;quot;ABC&amp;quot;. Then print the value using System.out.println(StringIdentifierHere). You could also do System.out.prinln(&amp;quot;ABC&amp;quot;), but we are focusing on creating variables.&lt;br /&gt;
&lt;br /&gt;
B. Do the above with a long set to 10000. Note that you have to say System.out.println(LongIdentifier + &amp;quot;&amp;quot;).&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;hr&amp;gt;&lt;br /&gt;
&amp;lt;!--[[Topic:Java|Back to Java Main Page]] | [[Introduction_to_Java|Previous Lesson]] | [[Java_Decision_Structures|Next Lesson]]--&amp;gt;&lt;br /&gt;
{{1lvlnav|[[Topic:Java|Java]]|[[Introduction to Java]]|[[Java Decision Structures]]}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Java]]&lt;br /&gt;
[[Category:Learning activities]]&lt;/div&gt;</summary>
		<author><name>70.17.19.237</name></author>
	</entry>
</feed>