In: Computer Science
Intro To Programming In Java(2nd Edition)
I'm currently trying to study and understand stuff better, but I need some help.
Can someone please help me define/understand the following:
Section 2.1
-Static Methods
-Control Flow
- Input Parameters and Arguments (single and multiple)
-Overloading
-Return Statement
-Side Effects
-Scope and Lifetime
-Pass-by-Value (Primitives) vs Pass-by-Reference (Arrays)
-Reason of Use: Organize Code
Section 3.1
-Strings (substring, charAt, split, indexOf, contains, length,
and == vs equals)
-Constructor
-Instance Methods
-Reference types(String and Arrays) vs Primitive Types(int,
boolean, char, double)
-Difference between Static and Instance methods.
Section 3.2
-API and UML
-Class
-Access Modifiers (private, public, and final)
-Instance Variables
-Instance Methods
-Scope and Lifetime
-Constructors
To understand any of these first you need thoroughly what is a class in Java, what is an instance of a class? When do you want to create an instance of a class and when do you not want to create an instance of a class?
A class is a just template or a skeleton that defines a combination of data objects and methods used in the class. The methods in the class are used to perform different operations on the different data objects defined in this class.
Now once we define the class how can we use it?
Class is just a definition. We can use class by creating objects.
So now what is an object?
Object is an instance of the class.
What does instance of a class mean?
Class is just a definition. At compile time, class definition is validated to see if the data objects use valid data types and methods use these data objects etc. Only when an instance of a class is created that's when memory is allocated for this instance called object. New memory is allocated for every object that is created for this class.
Now that we understand instance of a class. Let us try to understand how can we create an instance of the class?
This is where constructors come into picture. Instance of a class can be created only by using constructors.
What are constructors?
Constrcutors are used to create instance of an object. While creating an instance of the class we can also initialize some values to the different variables used in the class. The name of the constrcutor is the same as the name of the class and it needs to be public by default.
How can we initialize these values using constructors?
We can initialize these values using constructors by passing these values as arguments or as parameters to the constructor.
We can create multiple constructors with different arguments so that we can use appropriate one to create an instance of the class. This is also called Overloading. Method overloading is nothing but having different methods with the same name but different types or different number of parameters.
There are cases when some of the data and methods defined in the class can be used by classes or variables or methods outside of this class. Such type of methods are called utility methods. These methods will be defined as static. These static methods/variables are global to the class in which they are defined and can be accessed without creating instances for the class. Static variables or methods are not tied to an instance of a class. These are global to the class. So different instances of the class (different objects) can share and access these static variables and methods.
what is data type?
For any data defined as a variable we can declare the type for this variable. Declaring the type for the variable helps in assigning memory for that variable. For example when we declare a variable to be of type byte 8 bits of memory will be assigned to this variable during compile time. Data type can be primitive or non-primitive. Primitive data types are pre-defined, have fixed size and are common to most languages. Byte, short, int, long, char, float, double, boolean are primitive data types. In Java, byte needs 8 bits of memory, int needs 32 bits of memory etc. You can refer to this in your text book for understanding it better.
Non-primitive data types are the data that each object created for a class holds. This differs from one class definition to another class definition.
Now let us understand what is a method?
Method is a routine that usually performs one single task. Given the input, method processes that input and gets the output. This output can be returned back to the caller. A caller is also piece of code (usually in another method) that calls this method. Main use case of the method is it's reusability. That is for different inputs we can use the same method and get desired outputs.
For example, let us say we need to find the sum of two elements. We can get this done using '+' operator between two elements defined as variables in java. First let us understand what is a variable. Variable is a holder to
How is a method defined?
Method definition is called signature of the method. Method signature has method name, input (one or more arguments) and a return value. This return value is sent back to the caller which can use this value for doing further computations in it's own code.
What is call by value?
Call by value is when we pass the value of any variable as parameter to another method.
what is call by reference?
Call by reference us when the address of the variable is passed as a parameter to another method.
what will happen when we pass arguments to a method using call by value?
When we pass arguments to a method using call by value these values are used as input to the method and processing is done on the input using these values and the output is returned. The actual variables that were passed as arguments are used as read-only and they are not disturbed.
For example: Let us say we have three methods. First method is getResultUsingOperator and other two methods are sum and multiply.
Method signature for getResultUsingOperator: int getResultUsingOperator(int number1, int number2, char operator);
Method signature for sum: int sum(int num1, int num2);
Method signature for multiply: int multiply(int num1, int num2);
Code for getResultUsingOperator and sum and multiply are as listed below:
The values of the number1 and number2 in the method getResultUsingOperator will not change after calling sum or multiply methods even though their values are changed in sum and multiply methods because these arguments are passed using call by value. You will understand this when I explain the scope of a variable.
Reference types(String and Arrays) vs Primitive Types(int, boolean, char, double)?
As explained above primitive data types are pre-defined and have a fixed size. Reference types are for non-primitive types. A reference is to the variable/object is stored in memory instead of the value of the variable. For example, if we assign one variable to another variable.
For primitive type:
int a = 10;
int b = a;
In this case, a new variable b is created. 4 new bytes of memory is assigned to b and only the value of the variable a is copied into the variable b.
For Reference type like Array would look like this:
That is array 'b' is a reference to array 'a'. If we change value in Array 'a' it will be changed in Array 'b' also as both 'a' and 'b' are pointing to the same array.
In java always the variables are passed using call by value. For reference types the reference is called by value. So any value change for reference types passed as arguments will change the value of this reference type.
what is scope and lifetime?
Scope of a variable is the areas where all can the variable be used. Lifetime of a variable is the region in which the variable is active. That is region in which memory is still allocated for this variable. Usually the scope for a local variable is within the code block (code between '{' and '}') in which it is defined. Scope for a static variable is within the class in which it is defined.
Coming to access modifiers: Access modifiers like public, protected, private and default are used to define the scope for a variable or method.
UML: This is a pictorial representation of the class design of your program. UML can be used to draw a high-level represention for your application. This is useful to design your application before startiing coding. The dependencies between different classes within your application can also be defined using UML.
API: This is used to wrap all classes and packages together.
I gave you the concepts in general. There is lot of work from your side to learn these in-depth. I would suggest you to dig deep and try to understand the basics in general to understand all these concepts better.
Hope this helps to understand the text book material. Please let me know in case you did not understand something or you need more information on anything.