Lucky Cat Tutoring Services


CIT 111: Booleans & Other Primitive Data Types
September 1, 2009, 3:31 pm
Filed under: Uncategorized | Tags: , ,

Booleans: Can hold true or false, can be assigned any way that we want them, as long as the expression evaluates to “true or false”.

Example:

// A program for demonstrating boolean variables

public class TrueFalse
{
public static void main(String[] args)
{
boolean bool;
int a=18, b=12;
bool = true;
System.out.println(bool);
bool = false;
System.out.println(bool);
bool = a > b;
if (bool)
{System.out.println(“‘a’ is larger than ‘b'”);
}
}
}



Should come out to say:

true
false
‘a’ is larger than ‘b’

Process completed.

CHARS: are letter based only; can only hold a single character.

// This program demonstrates the char data type.

public class Letters
{
public static void main(String[] args)
{
char letter;

letter = ‘A’;
System.out.println(letter);
letter = ‘B’;
System.out.println(letter);
}
}

COMMON MISTAKES WITH CHARS:
-Errors occur when more than one character is used. Char can only use ONE.

-Double quotes “” around the single character makes it the wrong data type. Anything with “” is a STRING not a CHAR!! Single quotes must always be used


Chars and Integer relationships:
Letters can be assigned using numeric value for a letter in Java 6.

Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: