Showing posts with label Data Types. Show all posts
Showing posts with label Data Types. Show all posts

Sunday, March 9, 2014

Literal



A literal is any number, text, or other information that directly represents a value.


Literal format of String

eg.
String s1 = "This is the literal format of String";

"This is the literal format of String" is the String literal.

This is as opposed to:

String s2 = s1;

where s2's value is a reference to another variable.


Literal format of int

eg.
int num1 = 500;

500 is the int literal.

This is as opposed to:

int num2 = num1;

where int2's value is a reference to another variable.


For a listing of literals for all Java types also see:
"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )

Monday, February 25, 2013

Variable Syntax

Every variable has a (data) type and an identifier.

Variable declaration syntax (single line):

Type identifier = value;

      or (assignment statement in 2nd line):

Type identifier;
identifier = value;

Naming variables
There are rules and conventions around how Java variables are named.

A Java variable name:
  • should begin with a lowercase letter
  • can contain numbers
  • can contain an underscore
  • must be less than 255 characters long
  • must not contain spaces
  • must not begin with a number
  • if the name consists of more than word, capitalize the first letter of each word

Case sensitive
All names in Java are case sensitive. This means that a variable named numberOfClicks is different to a variable named numberofclicks.


Variable Declaration

When we create a variable in our program, we are said to be ‘Declaring a variable’. Variables are declared in methods. Methods are named blocks of code which perform a particular task. Variables may also be declared in the body of a class - that is, within the class declaration, but not within the body of a method.




When we declare a variable, we are telling the program to remember a value. Of course, there are many different types of data so we need to select the most appropriate variable type to store that value in.


The statement below is a variable declaration for some String data called myPassword. The value it is storing is “secret’.


String myPassword = “secret”;





Assigning values - The assignment operator

When we assign a value to a variable, we use the assignment operator - the equals (=) character. 

The assignment operator takes the value on the right and assigns it to the operand on the left side to form an assignment expression.


In the example below, the value “GateKeeper99” has been assigned to the String variable called myUserName.


String myUsername = “GateKeeper99”;


The assignment operator is also used to change the value being stored by the variable, as follows:


int numberOfUsers = 100;
numberOfUsers = 101;


We are also able to declare a variable in one statement and assign a value to it, in another statement.

As follows:


int xCoordinate;
xCoordinate = 500;


This technique is often used where we need to increase the scope of a variable.


Variable scope
A variable may only be referenced from within the region where it is declared. This means that if a variable is declared within a method, it is only accessible from within that method. Also, you cannot reference a variable before it is declared. If a variable is declared in the class body, this variable can be accessed from all methods in the class.




Variable lifetime

Variable lifetime refers to the length of time that a variable stores a value. Depending on the scope, a variable may have a lifetime that lasts for the time it takes a single block of code to execute, or for the entirety of program execution (more on that later).

What is a variable?


Variables are used to store values in a program. A value might be a name, a number, an address, and so on. These values can then be displayed or processed to create new information.

Think about a calculator. Each time you press a button, the calculator remembers what you have pressed. When you press the equals button, the calculator forms a mathematical equation using your series of key presses.

There are many types of values. Some values are made up of letters and numbers (such as an address) and some values are simply numbers. Each type of data can be stored in a program using a variable.

For instance, in a Java program, we could do the following:

String userName = “C2012000”;
String password = “monkey5000”;
String address = “30 Happy Place, Uptown”;
int numberOfLogins = 12;


The words String and int are types of Java variables - or data types.

Now we have 4 stored values that can be used again later in the program. Perhaps we need to print these values to the screen or perhaps we need to know where a letter must be mailed to. Perhaps we need to take an average of how many times a user has logged in over a particular time frame.

Alphanumeric & Numeric data.
Alphanumeric data is any collection of keyboard characters. It may contain letters, numbers & other special characters. Values such as these are stored in an variable that can hold alphanumeric values. This type of variable is called String.

String whatTheDogSaid = “Right on partner number 2!”;

A String may contain numbers, such as a house number in an address. But if we need to store a number so that we can do some kind of maths on that number, we need to use a variable type that can store numbers as numbers.

Numeric data are numbers. Generally speaking, data is considered numeric where it is being used for maths purposes - such as counting, adding, subtracting, and so on. Some numbers are whole numbers, such as the number of students on a bus. Some numbers contain a floating point, such as currency values, or body weight. For this reason, there is more than 1 type of variable that can hold numeric data. For instance, an int variable can store a whole number and a Double variable can store a floating point number.

int numOfStudentsOnBus = 22; 
double dogWeight = 12.2;

The number of students on a bus is going to be stored as a whole number, so we store it in a variable that can store whole numbers, such as int.

A number representing weight contains a floating point so we store this number in a variable type that can store floating point numbers such as double.

There are many data types in the Java language!

Here's a cat:


Sunday, June 3, 2012

the double data type

A double is a numeric data type. Suitable for storing floating point (decimal) numbers such as weight, length, etc. Any kind of number with a "point something' in it such as 7.6, 10.5, 1.2, etc.

Where a double has not been assigned a value, by default it's value is 0.0d.

The format for declaring a double variable is:

   Type identifier;

Where the Type is double and the identifier is weight:

   double weight;

...and where a value of 100.50 has been assigned:

   double weight = 100.50;


You can declare a double in one statement and assign it a value in another statement:

   double weight;
   // other statements...
   weight = 100.50;



See also:

"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )
"Java For Complete Beginners - Double variables" ( http://bit.ly/ZzYTJ9 )

Saturday, June 2, 2012

int data type



An int is a numeric data type

An int is a whole number between -2,147,483,648 and 2,147,484,647 (a pretty big number). The number cannot contain a floating point (no decimal).

Where an int has not been assigned a value, by default it's value is 0.


The format for declaring an int variable is:

   Type identifier;

Where the Type is int and the identifier is maxDamage:


   int maxDamage;

...and where a value of 100 has been assigned:

   int maxDamage = 100;


You can declare an int in one statement and assign it a value in another statement:

   int maxDamage;
   // other statements...
   maxDamage = 100;



See also:

"Java For Complete Beginners - Variables: the int type" ( http://bit.ly/ZA1aE8 )

Wednesday, May 23, 2012

Boolean

The boolean datatype can store the value True or False.


Where a boolean has not been assigned a value, by default it's value is false.

The format for declaring a boolean variable is:

   Type identifier;

Where the Type is boolean and the identifier is exists:

   boolean exists;

...and where a value of true has been assigned:

   boolean exists = true;

You can declare a boolean in one statement and assign it a value in another statement:

    boolean exists;
   // other statements...
   exists = true;

Tuesday, March 27, 2012

A handful of data types & variables

String
Any collection of keyboard characters.

Boolean
A true or false value.

Char
A single keyboard character.

int
A whole number between -2,147,483,648 and 2,147,484,647




Monday, March 19, 2012

Variables and Data Types

We use variables to store values in a program. Values can have be of different types - for example a value may be a String (any collection of keyboard characters) or a number such as an int.

Why would you want to store values? What kind of values?

Things like:

  • username
  • email address
  • age
  • URL
  • game stats
  • price


The format for declaring a variable is as follows:

Type identifier;

The format for assigning a value to a variable is as follows:

identifier = value;

The format for declaring a variable & assigning a value to it in one statement, is as follows:

Type identifier = value;

Here are some concrete examples:



http://www.functionx.com/java/Lesson02.htm

http://www.tutorialspoint.com/java/java_basic_datatypes.htm

Sunday, March 18, 2012

The char data type

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff'


"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )



What is a String?

DOWNLOAD THE CODE: IntroToStringPkg.zip

A string is a collection of any keyboard characters such as "I love chocolate 100 times more than you !!!"

Where a String has not been assigned a value, by default it's value is null.

In the example below, 2 Strings are created (lines 1 and 2). 

The 3rd statement concatenates (joins) the 2 Strings together (using the addition character). 

The fourth statement returns the number of characters in the String called firstName, by calling the String length() function (all Strings have this function, it returns an int). 

The fifth line finds the first character of the String by calling the String charAt() function (all Strings have this function, returns the character at the specified position). 

The final statement concatenates all of the values together and prints to screen.



The output:


A variable, such as String, can be declared in one statement and assigned a value in another statement, as below:

String myString;
myString = "This is a String!"; 

...or it can be declared and assigned a value in a single statement:

String myString = "This is a String!";







Literal String values (the actual charcters) are always enclosed in double quotation marks. 


See also: "Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/K4kbDz )

"String (computer science) - Wikipedia, the free encyclopedia" ( http://bit.ly/AgRVuH )

Here's a cat:



Default value of primitive data types

From http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html


Primitive Data Types

"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )

Tuesday, January 31, 2012

Primitive Data Types

"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )