Getting familiar with Java: Anatomy of a Class

Classes are forms of objects. These class files in Java are kept inside packages. Packages are type of library. We built properties of functions with java and these are library of functions and each segment of functions is capable for specific tasks or methods. Java provides opportunities for programmers to include or import source files to add it top of the class statements. For the most part of coding infrastructure you will have one package or library and there would be large number of class files.

This is not a rule of thumb. An application is capable to withstand large number of packages and several thousands of class files but still with a smart phone application these humongous number of files are not necessary. What makes java brilliant is that it does provides large number of pre-compiled packages or libraries as part of different class library and for this programmer finds it easier to start coding with these short cuts.

They do not have to go through such number of multifaceted coding as most of bulk codlings comes out right here. While drawing coding patterns for android, Google does provide number of pre-compiled packages related to building of android app. These pre-compiled packages should provide immense help to developers and these can be included using import statement and these generally stays at the top of your class files.

While writing code with any android developing platform such as Eclipse or with Android Studio you could find many such recommendations of classes that should be added while writing codes for application.

Anatomy of a Class:

The class name and the java file name must be the same. Suppose the class name is ‘HelloWorld’ then the java file name is ‘HelloWorld.java’. As I have told you earlier the class name always starts with capital letter. Package is the name of your application. Most cases an application consists of one package. There is hardly any application with more than one package. First we have package declaration. Then comes the import statement. That is importing of package into classes. This brings in various libraries into your class.

The benefit of import statement is that it allows you to use packages or libraries. Suppose you put import android.app.Activity helps you to implement the Activity methods within class.
After import you will define the class and then comes the properties and methods or in layman term the characteristics and tasks. These are the process of applications to work. Properties are pieces of information about your object or class and methods are dynamic instances with which objects can perform.

Inside class declaration you will have to include one or more methods that yours class needs to provide characteristics. These are functionality methods or functionality of classes. Define and construction of newer classes in addition to existing classes can be a possibility.

Variables:

Variables stores small pieces of information within it. Java stores information within variables. Each of variables contains data type. There are three types of data types such as string, integer and decimal. Variable are created within classes. But their scopes are determined through the places where you create your variables. The scope of variables never changes throughout the execution. When variables are created outside any methods and for this it can be created within classes. It is called as ‘global variable’.

Variable start with letter. It does not start with number. Variables are case-sensitive. The later characters of variables can be number or letters. Variables cannot use name of Java keywords and statement names. You should use simple English words so that you can create the readable variable format. You should be using pre-fixed characters such as small s for string variables, small b for boolean variables. Boolean variables are used for true or false statements. Small I for integer variable and big I for long variables.

This is called as Hungarian notation so that you could identify from the first small English letter of variable which variable you are using and that makes reading of codes extremely easier.

Supported data types for your variables:

The .String can be called as data types but in real it is a class. Another variable such as .float is a number having six decimal values. You can put capital F at the end of number or decimal value to tell Java that you are using a float integer. Java uses the mathematical functions of variables to go in certain way. This is called as upcast and it comes with one direction. It starts with byte and then short, and then int and then long and then float and then double. These processes cannot jump one over the another.

While performing mathematical operations the long number can come first and then the float can but not the long variable and then int variable. As it will generate compilation error. This is due to presence of upcast methods with variables.

Declaring variables:

Always use single quote (‘’) around char variable. Char variables are characters. Use double quote (“”)around the text variable. Text variables are sentences. Char variables are letters. Where as the numeric variables does not need any single or double quotes. Numeric variables consists of byte, short, int variables. Variables use upcast methods for mathematical functions. Variables can be initiated right way and then later on in accordance with conveniences the scope of variables can be defined right away.

Array:

When a variables has many values then it is called as array. When an array is being created one needs to define its limits or maximum number of values that it can take. The values of arrays are known as index. When we start an array for int 10 this means that the first index value starts with Array 0 as we know Java is a zero based language. The first and foremost question lies with variable is that why we should be using it. So, there is a particular reason for using of variable. Variable stores information within it so that we could work with these information later on when we need it.

In this way we can perform some basic mathematics operations with variables. When variable is declared we can provide value of variable at the same time.
int iValue = 1;

In this example we declare variable as integer and declare its value and end the statement with semicolon.

Increment and decrement operator:

In the case of increment and decrement operator there is a strong difference while using with arrays. When integer variable is a equal to 10, then a++ increment is 10 is the output as it provides the current integer values such as 10 as output. Where as in the case of as the output is 11 and in the case of ++a the output is 12 as ++a first makes addition and then provides the output.

Concatenation:

Strings or text variables can be added together to produce a sentence in the message box. This is known as ‘Concatenation’. String a can be ‘Hello’ and string b can be ‘World’ and the string c can be mixture of string a and string b and can be written as a+””+b; which should produce “Hello World” in the message box and this is known as ‘Concatenation’. This process we should be using more and more times while performing Java codings.

Casting and converting:

While writing programming codes many times you will need to use casting and converting methods which is converting variables from one format to the other. For example if you work with float variable and may end up with integer variable. If you want to cast string variable into a new data type such as int then wrapped that data types into parenthesis ().

In the case with casting and converting of variables all variables cannot be converted into another instance. We cannot convert text into int or Boolean to any other data types because of its characteristics.

On the other hand there are some variables when converted into another instances then it becomes meaningless altogether. For example converting 10000 into bytes (-127 to 128) is meaningless and that does not produce any meaningful results. While casting and converting some case might generate errors and you will have to take care of it so that these castings should be on the right line and should not provide any such errors.

We have already discussed that string information in Java is determined by double quotes (“”). If you want to escape from double-quote then you should be using special characters in these in order to provide escape sequences. There are different forms of escape sequences such as single quote, double quote, backslash, tab, backspace, carriage return, formfeed, newline.

In this chapter in order to be getting familiar with java we have learned in detail about, what are different anatomy of classes such as variables, strings, casting, concatenation, increment, decrement, array and much other information? It is time to practice these in order to be good at understanding these functionalities.

This entry was posted in Java on by .

Modified: 19th Oct 2017

About Mohan Manohar Mekap

Mohan Manohar is a blogger from India who founded Ittech back in 2007. He is passionate about all things tech and knows the Internet and computers like the back of his hand.