Sunteți pe pagina 1din 3

Introduction:

 
The  study  of  computer  science  is  based  on  the  manipulation  of  variables.  
 

A  Java  Primer:  
Java’s  structure:  

Class  
Method  
• Statement  
• Statement  

Method  
• Statement  
 
Data  types:  
int  
• Used  to  store  integers  

double  
• Used  to  store  numbers  to  decimal  precision  

boolean  
• true  or  false  

char  
• stores  single  letters  

String  
• Stores  sentences  and  words  
 
 
How  to  assign  values  to  variables:  
[data  type]  [variable  name]  =  value;  
For  example,  in  order  to  assign  a  variable  named  “integer”  the  
value  4:  
int  integer  =  4;  
Other  examples:  
char  letter  =  ‘d’;  
String  myName  =  “David”;  
double  approxPi  =  3.14;  
boolean  isItHot  =  true;  
etc..  
 
Arrays:  
You  can  think  of  arrays  as  boxes,  where  we  can  store  groups  of  
data.  You  can  have  arrays  full  of  ints,  booleans,  Strings,  doubles,  
etc…  
How  to  create  arrays:  
[data  type][]    [array  name]  =  new  int[[how  many  “slots]];  
or    
[data  type][]    [array  name]  =  {}  ;  
For  example,  to  create  an  array  full  of  5  integers  that  are  all  0’s,  
I  would  write:  
int[]  arrayOfIntegers  =  new  int[5];  
or  
int[]  arrayOfIntegers  =  {0,0,0,0,0};  
This  works  because  the  default  value  of  an  int  is  0.  
 
 
 
How  to  access  array  slots:  
We  may  want  to  access  individual  “slots”  of  an  array.  First,  we  may  
want  to  recognize  that  each  slot  has  an  index  that  begins  at  0.  
So,  for  this  array:  
int[]  coolArray  =  {1,2,3,4,5};  
We  would  do  this  to  assign  the  value  of  1,  or  the  value  at  index  0  to  a  
variable  named  “firstSlot”:  
int  firstSlot  =  coolArray[0];  
 
Methods  are  Functions:  
Methods  are  functions.  Simple  as  that.  
This  is  what  a  method  looks  like:  
public  int  awesomeMethod(int  awesomeNum)  {  
   //logic  would  go  here  
}  
The  spot  where  “int”  is  can  be  changed.  It  should  match  the  
data  type  of  the  variable  that  the  method  returns.  
The  name  of  the  method  is  “awesomeMethod”.  
The  method  has  one  parameter:  “awesomeNum”.  
Normally  at  the  end,  we  would  return  what  the  method  is  
supposed  to  find  (a  sum,  a  difference,  etc.).  
 
 
 
 
 

S-ar putea să vă placă și