AP Computer Science A

- Data Types & Variables


Learning Objectives:
Essential Knowledge:
Identifier/Variables
Data Type
Data Type Sizes
Data Types in Java
  1. Primitive data types:
  1. Non-primitive data types:

int
double
boolean
Other primitive data types not on your syllabus:
Variable naming convention in Java
How to declare a variable in Java
Types of Operator in Java
  1. Basic Arithmetic Operators
  2. Assignment Operators
  3. Unary +, -
  4. Relational
    (comparison)
    operators
  5. Logical Operators
    (including Unary !)
  6. Unary Auto-increment & Auto-decrement Operators
  7. Concatenation
  8. Bitwise Operators
  9. Ternary Operator
1. Basic Arithmetic Operators
Arithmetic Operator Precedence Rules
2. Assignment Operators
Operand
3. Unary Operators
Literals
Assign a value to a variable
Variable Declaration Variations
Variable naming "rules" in Java

Primitive data types in Java
int
Division
double
division
Conversion between primitive data types
  1. Implicitly - possible for automatic "safe" conversions.
  2. Explicitly - the programmer asks for values to be converted with a type cast
    (necessary for non-automatic "unsafe" conversions but also useful for "safe" conversions - see later)
    .
Safe / Automatic / Widening Conversions
int a = 123456789;

double x = a; // Is allowed. ✔

Unsafe / Non-automatic / Narrowing Conversions
double x = 11.0;

int a = x; // Is NOT allowed! ❌


/*

A type cast looks like this:

(requiredType) variable

Basically TypeCasting means:

"treat this variable as a (... data type) even

if it was declared as some other data type".

*/


int a = (int)x; // Is allowed. ✔


x = (double)a;

/* Type-casting is recommended even

for safe conversions, as it makes

it clear what is happening. */

boolean
double
division with variables
More Practice:
int x = 6, int y = 5;


double z = x/y;


double z = (double)x/y;


// Assume any one the declarations of z above.

y = z;


y = (int)z;

Type Casting Effects
int x = 6, int y = 5;

double z = (double)x/y;

How to round
(without the round() function which is not on your syllabus!)
double x = 2.2;

int xRounded = (int)(x + 0.5);

System.out.println(xRounded);

// 2.2 + 0.5 = 2.7 truncates to 2


double x = 2.6;

int xRounded = (int)(x + 0.5);

System.out.println(xRounded);

// 2.6 + 0.5 = 3.1 truncates to 3

Operator Precedence / Ranking

Local Variables
/*

All local variables must be given a value at some point during a program

otherwise you will receive a "variable might not have been initialized"

error if and when you try to access it

- this is the most important point at this stage.

Will the following code sections compile?

*/


int x;

.....

.....

System.out.println(x);



int x;

.....

.....

x = 0;



int x = 0;

.....

.....

.....


Exception
Arithmetic exception
Compile Time Vs Run Time Errors
alttext alttext
Debugging
Manually Tracing / White Box Testing
Line FirstNumber SecondNumber Total Output
05 10
06 12
07 22
09 22
... ... ... ...
... ... ... ...

Black Box Testing
Tracing / White Box Testing Example
int area = 0, spaceWidth, spaceLength, emptySpaces; // line 03

System.out.println(area /(spaceWidth * spaceLength - emptySpaces));
Line area spaceWidth spaceLength emptySpaces spaceWidth * spaceLength - emptySpaces
03 10 7 4 28
04 0


The value 0 tells us what?

Other Possible Tracing / White Box Testing Methods

User Input
/*

I will be using the latter 2nd way for basically the whole course

i.e. declare a variable as if the program is going to ask for user input

BUT instead, the 'input value' will be set directly and "hard-wired" into the code.

/*


// e.g.

int numInput1 = 6;

/* Simulates a program asking the user to enter a number, they

entering 6 and the program storing it in a variable 'numInput'. */

Writing your own programs:
  1. Of course you should use previous programs for reference, but write your code from "scratch"
    (do not copy and paste)
    .
  2. All your programs MUST have meaningful variable names not x, y, etc...
    • You may complain that I occasionally use these kinds of names but whenever I have, there was no context, they were just random lines of code used for concept demonstration.
      • If there is context within a purposeful program I also should use meaningful variable names.
      • As an additional point to prove the need for meaningful variable names exams love to use non-meaningful variable names just to make things complicated.
      • Although you may complain that writing meaningful variable names "wastes" time, you will save time in the long run as debugging time will be reduced, if not eliminated.
  3. Please NEVER use concepts or functions that are not on your syllabus.
    • If I haven't shown you something then this almost always means it is not on your syllabus so you should not use it.
      • However, for various reasons, there will be a few occasions where I will not introduce certain concepts or functions that may be useful until later on the course. In these circumstances I will accept if you use them earlier than I introduce them but only if they are actually on your syllabus.
    • In a real exam you are allowed to use things that are not on your syllabus, as long as they work, however, this is not recommended as mark schemes and readers may not recognise code that is not on your syllabus and as far as this course is concerned I personally will not accept concepts or functions that are not on your syllabus.
    • I also believe that not using advanced functions is more "pure" programming and gives you a clearer overall understanding of programming, particularly at this early stage.
  4. Finally look out for any requirements to write comments, if you don't write any comments when requested, you will lose marks. You have been warned!
1. Average
2. Mini-Calculator
3. Melon Packing Plant
4. Round
5. ASCII