Monday, January 3, 2011

Quick Recap – Chapters 1 to 5

We have successfully completed 5 chapters in our preparation towards the SCJP certification preparation. Let us do a quick recap to cover what we have seen so far.

Tip: You can bookmark this page (and the other quick recaps) to refresh just before your exam.

Chapter 1:

We saw that classes are the backbone based on which the java programming language revolves and everything in java is an Object (Java is an object oriented language)

• A class is like a template that describes the kinds of state and behavior that a particular object will support
• A class contains variables that refer to the objects state and methods that describe the behavior a class is expected to portray
• Java programs run on the Java Virtual Machine (JVM)
• Keywords are predefined words in the java language that tell the JVM to perform a predefined action
• You cannot use keywords are names for your variables or methods

Chapter 2:

We covered the declaration of various java entities like classes, interfaces etc in this chapter.

Java Source Files

• There can be only one public class per source file (a .java extension file)
• If there is a public class in a file, then the file name should be the name of the public class with a .java extension
• The package statement should always be the first statement in a java source file
• A file can have more than one non-public class

Class & Interface Declaration:

• A class can be created by using the “class” keyword
• The different types of classes we can create are final, abstract and the normal java classes
• An abstract class is a special kind of class that cannot be instantiated
• Any class that has one or more abstract methods has to be declared abstract
• An Interface defines a contract of what a class can do without saying anything about the exact implementation
• Any class that implements an interface has to provide implementation (code) for all the methods in the interface
• An interface is an 100% abstract class
• Variables in an interface are by default static and final which means they are constants
• An interface or an abstract cannot have final methods
• Every class has atleast one constructor that would get invoked during the process of object creation of the class
• If you don’t write a constructor, the compiler will build a default no-arg constructor for you
• Constructors cannot be abstract, static or final

Declaring Variables:

• There are two types of variables in Java – Primitives and Reference variables
• Static variables belong to the class and not to any object instance
• Variables declared inside methods are called local variables whereas ones declared inside a class (outside of any methods) are called instance variables
• Arrays are objects that hold multiple variables of the same type
• Though arrays are useful, collections like ArrayList or Vector provide more functionality than normal arrays

JavaBean Naming Conventions:
This is something I should have covered in the Declarations chapter. Since it is very small I thought I will add it to the quick re-cap chapter.

• All class names should start with a Capital letter and should follow camel case. Ex: TestStaticClass (See the capitalization of subsequent words)
• All method names should start with a small letter and should follow camel case. Ex: getNameOfEmployee()
• Method names should contain a prefix that gives an indication of what the method is supposed to do
• “get” is used usually for methods that return a particular value like getName() or getAge() etc
• “set” or “put” is used usually for methods that are used to modify the value like setName() or putDepositMoney() etc
• Methods that check a Boolean value must be prefixed with “is” like isTrue() or isValid() etc
• “add” is a keyword that is used usually with adding listeners to UI components like addEventListener() etc

Chapter 3:

In this chapter we had a detailed look at enumerations.

• enums can be used to refer to pre-defined values (constants) for your java program
• It is a good practice to have your enums in all CAPS to let other programmers know that they are constants
• enums cannot be declared inside methods
• Every enum has a static method, values(), that returns an array of the enum’s values in the order they’re declared

Chapter 4:

In this chapter we looked at the various Access Modifiers in java. They are public, private, protected and the default (no keyword) access modifiers.

• public is the least restrictive modifier whereas private is the most restrictive
• Though you may feel that public is better because all other classes can access your methods, that is seldom the case because a code that is visible to all other classes is not secure and that is not the best way to write your code. It is always best to provide just the appropriate level of access to methods and variables instead of having all of them public
• Protected and default access behave the same way as long as inheritance is not involved
• You cannot apply access modifiers to local variables

Chapter 5:

In this chapter we looked at the Non-Access Modifiers, the modifiers that do not alter the accessibility of java class members but that do alter its behavior.

• strictfp ensures that the floating point calculations in your code is in accordance to the IEEE 754 guidelines
• final classes cannot be inherited (sub-classed or extended)
• All java’s core library classes (ex: java.lang.String) are final
• An abstract class can never be instantiated
• A class cannot be both abstract and final
• Final methods cannot be overridden by any sub-class that might extend the original class
• Abstract methods have to be implemented (overridden) by the sub-class that extends the original class
• A method cannot be both abstract and final
• A synchronized method can be executed by only one thread at any time
• A native method is one which is implemented in a language that is not java (usually C)
• Static methods belong to a class and not any object instance of a class
• Static methods can call/invoke only other static methods/variables
• Final variables cannot be modified once they are assigned an initial value (not the default value)
• Transient variables will not be serialized when the object is serialized
• Using volatile keyword for variables in a multi-threaded application is a bad idea
• Static variables belong to a class and no matter how many objects you create of that class, there will be only one instance of the static variable
• Method arguments can be declared final which ensures that those variables will not be modified inside the method code

That rounds up the quick recap of the first 5 chapters of this SCJP series.

Previous Chapter: Chapter 5

Next Chapter: Self Test Chapters 1 to 5

No comments:

Post a Comment

© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.


Google+ Followers