Skip to content

Instantly share code, notes, and snippets.

@radwomenunazhang
Last active October 12, 2020 04:42
Show Gist options
  • Save radwomenunazhang/bca1fb57addb4115e8176910bf9c4370 to your computer and use it in GitHub Desktop.
Save radwomenunazhang/bca1fb57addb4115e8176910bf9c4370 to your computer and use it in GitHub Desktop.
public with sharing class WeekOneHomework {
public static void introToPrimitives() {
//Primitives are the simplest elements in a programming language
/* A selection of primitives in Apex:
Integer: A number that does not have a decimal point, like 1 or 789 or -34
Decimal: A number that includes a decimal point like 1.34 or 456.78907654
String: Any set of characters surrounded by single quotes, like 'Apple' or 'I Love Strings' or '2 Legit 2 Quit'
Boolean: A value that can only be true, false or null
Id: Any valid 18 character Force.com Id (if you set an Id to the 15 digit value it automatically converts it to the 18 character version)
Ok, there's more than those, and you can read all about it here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_primitives.htm
But that's enough to get us started. Let's start by declaring some variables and initializing them with values
//Wait, what do "Declare" and "Initialize" mean?? Let's explain by doing:
*/
Integer i = 2; //Ok, so what we've done here is tell the computer that we are creating an instance of an integer
//(Remember our list of primitives? Variables have to be a type of primitive.
// We are going to call this instance (aka: variable) "i" and we're going to initialize (aka: assign) this variable with the value 2.
//When people say Apex is strongly typed, they mean that we have to tell the computer exactly what kind of variable we're creating every time.
//Let's make some more
String mySampleString = 'This Awesome String is full of Stringy goodness';
//ok, we told the computer that we are creating a variable that can hold any combination of characters.
//Now we have a variable that is named mySampleString and which has stored the sentence you see above.
//What if we declare a variable but don't initialize it with a value?
Decimal dec;
/*We can totally do that, but that there is no value assigned yet and you'll need to make sure you assign a value later on
in your code. If you don't and you try to use the value of that variable later you'll get an error.
let's give it a value so we don't get into trouble. */
dec = 12.4;
/* Hey, I didn't have to use the word Decimal this time. Why?
Since I declared the variable up on line 31 and named it dec, the computer now knows it's a decimal. I don't have to tell it again*/
//One more note on syntax: what's with all the semi-colons?
// a ; is how you tell the computer that you're done with a line of code. Get used to typing lots and lots of semi-colons!!
}
public static void primitivesExercise() {
//You do this part!
//1. Declare three primitives variables, an Integer, a String and a Decimal
Integer myInteger;
String myString;
Decimal myDecimal;
//2. Assign values to your three new variables
myInteger = 9;
myString = 'I am learning coding.';
myDecimal = 3.1415926;
//3. Print out your variables to the debug log
System.Debug('Print the assigned value :' +myInteger);
System.Debug('Print the assigned value :' +myString);
System.Debug('Print the assigned value :' +myDecimal);
//4. Declare an integer variable and assign a value in the same line
Integer myFavoriteNumber = 9;
//5. Print out your integer to the debug log
System.Debug('Print number:' +myFavoriteNumber);
}
}
@chrisyojay
Copy link

Wanted to leave feedback (a bit late though!) since you might not be able to save and test this code.
On lines 49-51, the end of the statements are missing a semicolon (;).

@chrisyojay
Copy link

Once that is fixed, then lines 53-55 will show an error that will block saving. Variables cannot be declared twice in the same scope*. Here's an example of something that won't save:

Integer sameInteger;
Integer sameInteger;

What does scope mean? for our purposes-- it is anything within the same {}. Back to lines 49-55, the variables myInteger, mySampleString, and myDecimal are all in the scope of the primitivesExercise() method, and can only be declared once.
How to remember declaration? Providing a type (like Integer, String, etc) on the left side of the equal side
So what can you do?

// Declare an integer
Integer differentInteger;
// Assign a value to that integer
differentInteger = 9;

@chrisyojay
Copy link

Please let me know if anything is still unclear! I saw you remembered all semicolons for Week 2 homework, so good progress :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment