Java Variables and Expressions                 

   Contact
   Search
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Messages
   Chat
   MCSE
   Linux
   Cabling   
   ActionScript
   Downloads
   E-Cards   
 
    
    
Java 2  Java 3  Supplementary  Events  AWT Project  AWT Project  Language  Swing  Sound/Images

To get started, first read about Object Oriented Theory from the C++ tutorial. You must use a particular data type to hold a particular value. Variables are containers for storing different types of data. They come in different types and can hold objects of different sizes based on the variable container's size. The data type of a variable tells the compiler how much room to set aside in the computer's memory to hold the variable object's value. On any computer, each variable type takes up a single, unchanging amount of room. Like C++, Java is a strongly typed language, and thus it is very important to store the right kind of data in the right kind of container. Some types of variables: int, float, double, long, char, String.  Unlike C++, when variables are declared they need to be initialized in Java. Additional information about variables and how they use different amounts of memory can be found HERE .

T
he traditional "Hello World" application:

PROJECT 1

public class GermanyGreeting
{  
       public static void main(String args[])
      
{
          
System.out.println("Hello.  My name is Charles Germany. What's yours?");
       }
}

Notice: The main() function must be declared inside a class. The main() function takes command line arguments. The function System.out.println("") displays output to the console. For many features in Java, you must import classes. Importing is like the C++ "#include":

To use prewritten classes you can:
1. Use entire path:     
java.util.Date birth = new java.util.Date();
2. Import class:        
import java.util.Date;
3. Import package:      
import java.util.*;   

PROJECT 2

public class DeclareAndInitializeVariables
{  
       public static void main(String args[])
      
{ 
              int y = 50000;
              int z = 100000;

              System.out.println("Your gross pay is " + (y + z) + "\n\nYou wish!");
      
}
}

Notice: That variables in Java must be initialized when they are declared. That strings can be concatenated (added together with variables and other strings) using the "+" operator.


PROJECT 3

public class UsingFinal
{  
       public static void main(String args[])
      
{ 
              int hours = 40;
              final float payrate = 12.0;
              double commission = 100.2358;  
 

              System.out.println("Your earn: $" + ((hours * payrate) + commission) + ".");
      
}
}

Notice: Variables can hold different sized values. The keyword "final" operates as the keyword "const" in C++, it menas the value can not change. Using this practice reduces errors in programming for constants that are not supposed to change. It is unfortunate that this applies to the variable "payrate". :-)

PROJECT 4

public class UsingAString
{  
       public static void main(String args[])
      
{ 
              String DaName = "";    
              DaName = JOptionPane
.showInputDialog(null, "Your name?"));
              System.out.println("Hello" + DaName + "!");
      
}
}

Notice: That a String object receives text input. That the JOptionPane.showInputDialog() function
displays a pop up window allowing user input and returns that value to the String "DaName".

PROJECT 5

public class UsingA_char
{  
       public static void main(String args[])
      
{ 
              char DaLetter = 'z';    
              System.out.println("The letter is: " + DaLetter + ", home slice!");
      
}
}

Notice: That a char, unlike a String, only holds one character. That a char is initialized and assigned to with single quotes ('')as opposed to double quotes ("").

PROJECT 6

import java.util.*;

public class UsingTheDateObject
{
       
public static void main(String args[])
       
{   
           
Date = new Date();
            System.out.println("Current month is " + toDay.getMonth());
            System.out.println("Current day is " + getDate());
            System.out.println("Current year is " + toDay.getYear());
            toDay.setDate(toDay.getDate()+60);
            System.out.println("Sixty days from now is ");
            System.out.println(toDay);
       
}
}

Notice: That the Date object must be instantiated with the keyword "new" before it is used.

More About Type Casting - For those who aren't C++ veterans, there's this thing called "type casting". When I first heard the expression "type casting", I thought it was like where Patrick Stewart and William Shatner, no matter what they star in after Star Trek, will nevertheless be known by all as the "Captain of the Enterprise".  Leonard Nimoy, even when hosting "In Search Of", was still seen by all as "Spock".  My mentors found my confusion enormously amusing.  So if you know that this is not what we mean when we talk about "type casting" in C++ and Java, then you are already farther ahead that I was when learning these languages, though that's not saying much.  Where programming languages are concerned, "type casting" has a slightly different connotation.  Java implicitly converts nonconforming operands to a unifying type.  Type casting explicitly converts and overrides the unifying type imposed by Java.  To type cast, place the desired result in parenthesis followed by the variable or constant to be cast. Example: double exam;    float test = (float) exam /4;   Of course, we would loose some data here as it would be truncated.

With variables, you may find these functions useful:

Common Math class methods:
abs(x) - absolute value of x
random() - random double number
round(x) - rounds closest integer to x
sqrt(x) - squareroot of x
pow(x,y) - x raised to the y power
cos(x) - cosine of x
sin(x) - sine of x
tan(x) - tangent of x


Java 2  Java 3  Supplementary  Events  AWT Project  HOME  AWT Project  Language  Swing  Sound/Images