Difference between revisions of "Java Fundamentals"

From CompSciWiki
Jump to: navigation, search
m (Typo)
(Don't know what I did so I have reverted it! oops....)
 
Line 1: Line 1:
{{Template:1010Topic
+
{{1010Chapter|ChapterNum=2
|Chapter_TOC=[[Java Fundamentals]]
+
|Picture=Wiki_chars03.jpg
|Previous=[[Comments]]
+
|Introduction=In Chapter 1, you saw an introduction to Java and the structure of a basic program.  With this knowledge we can now move into further detail about Java fundamentals that will enable you to write programs that do more than output 'Hello World'!
|Next=[[Input/Output using JOptionPane]]
+
|Body=
+
  
==Introduction==
+
__NOTOC__
Programs would not be very interesting if they could not interact with the user.  This section covers how to use the Java <code>System.out</code> function to display a message to the user within a console window.  We will start off by display general text, then move on to combining text and numbers to create complex output.
+
  
==Giving the User Information==
+
|Body=
 
+
[[Image:Console.png|left]]  One method of interacting with a user is through the console, which is command line interface. Java can write to the console using methods within the System package. Sending information to the user through the command line interface is a lot like printing to paper. You tell the computer what to print and then it prints your message to the console for the user to read.
+
 
+
The System package contains two methods that allows you to write a [[Strings|String]] to the console window: <code>System.out.print</code> and <code>System.out.println</code>.
+
 
+
===<code>System.out.print</code>===
+
 
+
The <code>[[http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#print(java.lang.String) System.out.print]]</code> method call is use to write a [[Strings|String]] to the console window.  As you can see in the code block below, to write to the console all you need to do is put the [[Strings|String]] inside the brackets.  When we look at the output however, we see that the command line prompt is right next to the text we are trying to show the user - this makes it hard to read.  Although there are situations where this is beneficial, it would be better to use <code>System.out.println</code>.
+
 
+
====Code====
+
<pre>
+
public class PrintDemo
+
{
+
    public static void main(String args[])
+
    {
+
        System.out.print("Hello World");
+
        System.out.print("Hello World");
+
        System.out.print("Hello World");
+
    }
+
}
+
</pre>
+
 
+
====Output====
+
 
+
<pre style="background-color:black;color:Chartreuse;">
+
C:\>javac PrintDemo.java
+
C:\>java PrintDemo
+
Hello WorldHello WorldHello WorldC:\>
+
</pre>
+
 
+
===<code>System.out.println</code>===
+
 
+
The <code>[[http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println() System.out.prinln]]</code> method call is almost identical to the <code>System.out.print</code> method, except it add a line terminator, new line character, to the end of the [[Strings|String]].  As you can see in the code below it is used exactly the same way.  In the output, each [[Strings|String]] appears on its own line.
+
 
+
====Code====
+
 
+
<pre>
+
public class PrintDemo
+
{
+
    public static void main(String args[])
+
    {
+
        System.out.println("Hello World");
+
        System.out.println("Hello World");
+
        System.out.println("Hello World");
+
    }
+
}
+
</pre>
+
 
+
====Output====
+
<pre style="background-color:black;color:Chartreuse;">
+
C:\>javac PrintDemo.java
+
C:\>java PrintDemo
+
Hello World
+
Hello World
+
Hello World
+
C:\>
+
</pre>
+
 
+
==Printing More Than Just Text in Quotes==
+
Now that you know how the two methods work, here are some examples demonstrating how to print more than one kind of argument. Remember, ultimately, the type of the argument in the print methods must always be a [[Strings|String]]. Even though you probably haven't worked much with variables you can still get the general idea here.
+
 
+
<pre>
+
public class PrintNum
+
{
+
    public static void main(String args[])
+
    {
+
        int num = 5;
+
        System.out.println("The number is " + num);
+
    }
+
}
+
</pre>
+
 
+
<pre style="background-color:black;color:Chartreuse;">
+
C:\>javac PrintNum.java
+
C:\>java PrintNum
+
The number is 5
+
C:\>
+
</pre>
+
 
+
There are a couple key things going on here. First of all, the argument contains a [[Strings|String]] in quotes, but also an [[Common Primitive Variables|int]] variable. Secondly there is a plus sign between the two. These two things work together to make things happen.
+
 
+
Firstly, the plus sign is telling the compiler to join the two items, the [[Strings|String]] in quotes and the [[Common Primitive Variables|int]] variable, and together they makeup a single argument for the println method. Secondly, the println method exclusively takes a [[Strings|String]] argument. What happens is, by using the plus sign to tell the compiler to join the two items, you are also telling it to implicitly [[Casting|cast]] the [[Common Primitive Variables|int]] variable to its [[Strings|String]] value. For all [[Common Primitive Variables|int]]'s this is just its value (i.e. 5 is "5" and -987654321 is "-987654321" and so on). The [[Common Primitive Variables|int]] variable is first cast to a [[Strings|String]] so it is allowed to be joined to another [[Strings|String]], and together they form a single [[Strings|String]] argument for the print method.
+
 
+
You can join as many items together as you wish, however it quickly becomes unreadable.
+
 
+
The next likely question is what if we want to print a number, but no words? How do we convert the int to a [[Strings|String]]? The answer involves a little trick as shown below:
+
 
+
<pre>
+
public class PrintNum
+
{
+
    public static void main(String args[])
+
    {
+
        int num = 99;
+
        System.out.println("" + num);
+
    }
+
}
+
</pre>
+
 
+
<pre style="background-color:black;color:Chartreuse;">
+
C:\>javac PrintNum.java
+
C:\>java PrintNum
+
99
+
C:\>
+
</pre>
+
 
+
This code works exactly the same way, except this time we don't have anything between our quotes. Why do we need this? Why can't we just have the [[Common Primitive Variables|int]] part? Well if we just gave the println method an int argument, it would give us an error because it only takes [[Strings|String]]. By including the empty [[Strings|String]] "" we are telling the compiler we want to [[Casting|cast]] the [[Common Primitive Variables|int]] variable to a [[Strings|String]] again, but this time we want to join it with nothing, we want the number by itself.
+
 
+
<pre>
+
public class PrintRocks
+
{
+
    public static void main(String args[])
+
    {
+
        String text = "Comp 1010 Rocks!";
+
        System.out.println(text);
+
    }
+
}
+
</pre>
+
 
+
<pre style="background-color:black;color:Chartreuse;">
+
C:\>javac PrintRocks.java
+
C:\>java PrintRocks
+
Comp 1010 Rocks!
+
C:\>
+
</pre>
+
  
The two above examples of code would produce identical results. There are several ways to give the print methods a [[Strings|String]] argument. We can define it in a [[Strings|String]] variable, like the first example, and give it the variable, or we can define it right in the argument itself as in the second example.
+
==[[Comments]]==
 +
==[[Output using System.out.]]==
 +
==[[Output using JOptionPane]]==
 +
==[[Input using JOptionPane]]==
 +
==[[Input using Scanner]]==
 +
==[[Variables and Literals]]==
 +
==[[Common Primitive Variables]]==
 +
==Operators==
 +
===[[Arithmetic Operators]]===
 +
===[[Relational Operators]]===
 +
===[[Logical Operators]]===
 +
===[[Increment and Decrement Operators]]===
 +
==[[Strings]]==
 +
==[[Casting]]==
 +
==[[Named Constants]]==
 +
==[[Your_First_Java_Program_Review_Questions_and_Exercises|Review Questions and Exercises]]==
  
  
More information on creating Strings expressions can be found in the [[Strings#String_Expressions|String]] section.
+
|PChapterNum=1
 +
|PChapterLink=[[What_is_Programming%3F|What is Programming?]]
 +
|NChapterNum=3
 +
|NChapterLink=[[Calling Methods]]
 
}}
 
}}

Latest revision as of 15:52, 25 November 2011


Wiki 1010 Table of Contents

Wiki chars03.jpg

Chapter 2

In Chapter 1, you saw an introduction to Java and the structure of a basic program. With this knowledge we can now move into further detail about Java fundamentals that will enable you to write programs that do more than output 'Hello World'!


  Write a Program a Day Case Studies

Comments

Output using System.out.

Output using JOptionPane

Input using JOptionPane

Input using Scanner

Variables and Literals

Common Primitive Variables

Operators

Arithmetic Operators

Relational Operators

Logical Operators

Increment and Decrement Operators

Strings

Casting

Named Constants

Review Questions and Exercises




Chapter 1: What is Programming? Table of Contents Chapter 3: Calling Methods