Tuesday 14 April 2015

Java Primitive Variables

Primitive Variables 
A variable is an item of data named by an identifier
• Variable declaration is manipulation of the computer’s scratchpad (RAM) 
• We are reserving a space in the scratchpad and giving that space an easy-to-use name 

Examples: 
• int x = 0; 
• float f = 3.14159265; 21

Fixed Point Data Types  
Byte - byte b = 16; 
• 8-bits, -127 to 127 n Short - short s = -1543; 
• 16-bits, -32767 to 32767 n Int - int i = 100340; 
• 32-bits, -2 billion to 2 billion n Long - long l = -123456789123; 
• 64-bits, absurdly large numbers

Fixed Point Data Types
Used when representing integral numeric data (like 4 or 5) n Common misconception: 
• Fixed point types can/is not used to represent fractional values n Used to represent data where the decimal point position stays constant 

Example: 
money … $18.45 22
Floating Point Data Types n Used when data may take on wildly different values or when scientific precision must be preserved n Float 
• float f = 3.14159265; 
• 32-bits (max value ̃10^38) n Double 
• double d = 5.6243*Math.pow(10,250); 
• 64-bits (max value ~10^308)

Why use fixed point? 
Why bother with implicit decimal points? 
• You might forget about the point… 
• Somebody else might modify your code…  
First guess: it’s the size 
• 8 bits versus 32 or 64 bits…
• No… because of alignment issues n The real reason… SPEED! 23 
Fixed vs. Floating Point n On a MIPS R4000 class processor (found in 1990 SGI Indy’s and Y2000 PDAs like the Casio Cassiopeia)… 
• Floating point division takes ~ 70 cycles 
• Fixed point division takes ~ 13 cycles n This is even more apparent with SIMD instruction sets…
• MMX/SSE/3DNow, etc. can improve fixed point performance by 4 to 16 times!

Other Data Types  
Boolean
• 1-bit fixed point type 
• Use the words “true” and “false” to assign and compare values n Char 
• Holds a single unicode character • 16-bits (unlike the “usual” 8-bit ASCII) n Reference 
• Called pointer in C/C++… this holds an address in memory 24

Literal Data 
How can you tell if 12 is a byte, short, int or long?  By default, literals w/o a decimal point are int and with a decimal point are double 
• You can use 12345L to make a long 
• 12.3456F can be used for float 
• Byte/Short don’t have equivalents

No comments:

Post a Comment

CakePHP Date Picker

Folder Structure: Controller:   File location - cakephp/src/controller/HomepageController.php namespace App\Controller; use...