Skip to content

Instantly share code, notes, and snippets.

@yareally
Created September 12, 2010 20:40
Show Gist options
  • Save yareally/576436 to your computer and use it in GitHub Desktop.
Save yareally/576436 to your computer and use it in GitHub Desktop.
/*** Wes's Java Cheat Sheet Version 1 ***/
/*** Last Updated 2010-09-11 ***/
/*** Location online http://gist.github.com/576436 ***/
/*
* Hopefully this is a big help for you. I know I could have used something like this.
* This should sum up everything you do in java for at least the first few weeks of it.
* It may not be perfect, but can always add to it or add to it yourself at the link above.
*
* Things that will come afterwards I would need to add to this would be like how to use
* queues, stacks, arraylists and another things that are basically just normal arrays
* with more specific tasks.
*
* Also could add some stuff on how to use public/protected/private for ecapusulation (fancy
* word they will probably use for hiding parts of the code from someone using it). As well
* as getting more detailed on classes themselves.
*
*/
/** Every Java program will start with something similar below if you
are intending on making the main part of the program that runs everything
(equivilant to the "main" in c++).
For now, you can ignore what String [] args does (see near the start in
the program below for what I mean), probably wont use it anytime soon or often.
(if you care, it's just an array that holds variable names to anything you typed
on the command line, so you can access it in the program if needed).
Everything in java is a class, unlike in c++, where your functions and such were classes
only if you defined them to be. Although everything in java is a class, it doesn't
make it any different really than if it didn't have them always by default. You still have
your main where you call to other functions and such elsewhere like in c++.
**/
/**************** The Anatomy of an Java Program ****************/
// comments can be like this.....
/* or they can be like this...... */
/* or if you want to be fancy,
multiline like this
*/
/** and if you like using lots of *,
feel free, lol, makes no difference.
**/
// the IMPORT lines are similar to how you import stuff in c++
import java.util.Scanner; // library to read in user input (like cin >> in c++)
// assume this will always be set to public for now
public class name_of_lab_or_project
{
//this will always be set to public (if it's the main)
/***** START OF THE MAIN AREA OF THE APPLICATION *****/
public static void main (String [] args) // see note somewhere above about this line
{
/********** PRINTING OUT STUFF **********/
//same as saying cout << "Blahhhh" << '\n';
System.out.println("Blahhhh");
// same as above, but doesnt add '\n' (new line)
System.out.print("Blahhhh");
/********** END OF PRINTING OUT STUFF **********/
/********** GET USER INPUT **********/
System.out.print("Enter your name: ");
// open up the thing to read in user data(same as cin in c++)
Scanner keyboard = new Scanner (System.in);
// read in the user name they typed by getting the whole line
String yourName = keyboard.nextLine();
System.out.println("Your name is " + yourName);
/********** END OF USER INPUT STUFF **********/
/********** DECLARING AND USING VARIABLES **********/
int blah = 10;
float someFloat = 10.423;
double someDouble = 13.3122
String someString = "blahhhh";
char letter = 'b'; // note the '' instead of "" for characters
boolean thisIsTrue = 1;
boolean thisIsAlsoTrue = true;
// creates an array with 10 buckets
int[] someArray = new int[blah];
// you can also make an array like this too:
int[] anotherArray = new int[10];
// or like this:
int[] oneMoreArray;
oneMoreArray = new int[10];
// string arrays are created teh same way:
String[] stringArray = new String[15];
// or you can make arrays like this so it has variables to start:
String[] myStringArray = new String[] {"Java...", "at least", "it's not", "c++"};
int[] fibbonaciArray = new int[] {1, 2, 3, 5, 8, 13, 21};
// sets the zero (first) bucket in the array to 42, instead of 1
fibbonaciArray[0] = 42;
/********** END OF DECLARING AND USING VARIABLES **********/
/********** CONDITIONAL STATEMENTS **********/
if ( anotherString.length() > 0 )
{
// if the new string has more than zero characters in it do this
// print out the string and a message stating what it is
System.out.println("Your new string is: " + anotherString);
}
else if ( anotherString.length == 0 )
{
System.out.println("Your string is empty");
}
else
{
// string is null or some other weird error making it less than zero
System.out.println("ERRORRRRRRRRR >:( you fail");
}
// switch is like more simple if/else, remember blah was 10 from above
switch (blah)
{
// do this if blah is equal to 0
case 0:
System.out.println("this is the first case");
break; // exits out of the switch when it sees break;
// do this if blah is equal to 5
case 5:
System.out.println("blah is equal to 5");
break;
// do this if blah is equal to 8
case 8:
System.out.println("blah is equal to 8");
break;
// do this if blah is equal to 10
case 10:
System.out.println("blah is equal to 10");
break;
// default case if none of the above work (not necessary to have a default unless you want one)
default:
System.out.println("no matching case, using default one");
break;
}
boolean lessThanZero;
// ternary statment (this is like a very simple if/else, good for setting variables)
lessThanZero = (blah < 0) ? true : false;
//the above is the same as doing this with an if/else:
if ( blah < 0 )
{
lessThanZero = true;
}
else
{
lessThanZero = false;
}
/********** END OF CONDITIONAL STATEMENTS **********/
/********** LOOPS **********/
for ( int count = 0; count < anotherString.length(); count++ )
{
/* prints out each character starting from the first, one at a time
one on each line.
*/
System.out.println(anotherString.charAt(count));
}
// now the same loop as above, but as a while loop....
int count = 0;
while ( count < anotherString.length() )
{
/* prints out each character starting from the first, one at a time
one on each line.
*/
System.out.println(anotherString.charAt(count));
count++;
}
/** does the same as the WHILE loop above, but does it at least once
regardless of whether count < anotherString.length() or not.
DO WHILE loops are not used very often.
*/
do
{
System.out.println(anotherString.charAt(count));
count++;
}
while ( count < anotherString.length() ); // note the semicolon
/*------ NEW TYPE OF LOOP NOT IN C++ ------*/
/* the below loop is called a foreach loop */
// create an array to loop through....
int[] myIntArray = new int[] { 1, 2, 3, 5, 8, 13 };
// the foreach loop
for ( int someSingleInt : myIntArray)
{
// prints out each variable in the buckets in the array
System.out.println(someSingleInt);
}
// the above loop is the same as doing this:
for (int count = 0; count < myintArray.length(); count++)
{
System.out.println( myintArray[count] );
}
/********** END OF LOOP STUFF **********/
/********** FUNCTION STUFF (functions are below the main) **********/
// a function where you want to return a variable you worked with...
String userString = "blahhhh";
// makes a string with the characters "blahhhh blah" in it
String anotherString = doSomethingInAFunction(userString);
// a function where you just want to do some work but not return anything...
// calls to the function way down below to print out the string we pass to it
printAString(anotherString);
/********** END OF FUNCTION STUFF **********/
} // end of the main area
/********** END OF THE MAIN AREA OF THE APPLICATION **********/
/********** THE FUNCTIONS TO GO WITH THE FUNCTION CALLS ABOVE **********/
/** FUNCTION (METHOD) example to go with the above....
note: for clarity, userString has the same name as in the main,
* but it can be whatever you like here.
*/
public String doSomethingInAFunction(String userString)
{
/* adds the word "blah" to the end of String userString
and returns it to the main (concat is built into java,
short for concatonate, in case it's not obvious )
*/
return someString.concat(" blah");
}
/** A no variable (void) FUNCTION (METHOD) example to go with the above....
note: for clarity, userString has the same name as in the main,
* but it can be whatever you like here.
*/
public void printAString(String userString)
{
// print out some string and that's it
System.out.println(userString);
}
/********** END FUNCTIONS TO GO WITH THE FUNCTION CALLS ABOVE **********/
} // end of the class (note that functions go inside of here, but outside of the main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment