Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
Java: The Complete Guide for Beginners to Learn Java Programming Fast
Page
3
Table of Contents Introduction Chapter 1- What is Programming? Chapter 2- What is Java? Chapter 3- Conditions and Control Flow Chapter 4- Object Oriented Java Chapter 5- Data Structures Chapter 6- Helpful Hints and Resources Conclusion
Page
4
Introduction The aim of this book is to help assist you with learning how and what programming and Java are. Most of us have heard the terms “computer programming” and “coding” but may not know much else. Computer programming has been around since the invention of computers—as it is the manner in which a computer is organized in order to run various programs. Coding is the ability for someone to speak the language of the computer through a computer programming language. Just like with humans, computers have multiple languages they can use to speak to each other. If I have gained and peaked your interest with this first paragraph—this book is most definitely for you. Most of us are greatly interesting in why and how we could learn how to gain more knowledge of control over a certain subject or thing—computers are actually quite easy once we know the basic philosophies! Through the course of this book, I hope to have you comfortable to the terminology and basic functioning of computers in the fields of programming and using Java within the coding and programming world. Although I cannot promise you will know everything about Java and the computer—I do promise that you will have enough skills and comfort with basic principles you will be able to play in your own computer sandbox (more about that in the first chapter)! Towards the end of the book, I go over a lot of helpful hints and resources that can be used to get a greater grasp on Java itself and how to use it. Although some of the many hints and terms may not make much sense to you know, remember that you are building a foundation and resource list to come back to as you run into speed bumps in your coding and programming journey. I encourage you to read through all of them and try to retain what you can—and come back for the rest when you need it. I encourage you to follow along with some of the ideas in this book in the Java program itself. Using this book as a how to guide through your programming experience that can be used in tandem to create the most long term memory for learning new things. Make sure to try things multiple times to make sure you understand the concepts completely and can easily replicate them at any given time, even if you need to look up a resource to be able to find exactly what you are looking for. Although it is not humanly possible to memorize everything, this book should give you general knowledge you can use in order to feel like you have a firm grasp on the beginning concepts of Java. With that, I look forward to you working your way through these chapters. The aptitude for learning skills like computer programming are really only inhibited by the amount of work you want to put into them. We shall start the adventure into programming with Java with an entire chapter devoted to the basic terms and functions of programming, giving you a firm foundation in the virtual computer world.
Page
5
Chapter 1- What is Programming? More than likely, you have heard the term “programming” before in your life, either on the internet or in a conversation with someone about technology. If you are like me, you may have just smiled and nodded your head, in order to appear that you may know what the person is talking about— rather than admitting that you have absolutely no firm knowledge of what it entails. When it comes to computer programming, Java is the language we are looking at within the world of computer programming. It is important to have a basic understanding of programming before we can start getting into the nitty gritty of what Java is. Most people can operate a basic graphic interface on a computer by clicking on icons or typing in a Word document to prepare a paper for school or a resume to gain employment. However, the original computers did not have these graphic icons and looked more like blocks and blocks of different texts strung together. In order to understand what all of these countless characters on the screen meant, people had to learn how to speak the language of the computer. This is what programming (or computer programming in formal terms) really boils down to—an understanding of how to speak a computer’s language to get it to carry out the tasks you want it to do. When you program, you always want to remember that although there are language rules and structural syntax rules that only allow you to do certain things, you can always improve upon the existing if you want to. There may be something that works well for others and not so much for you. If for whatever reason the way something is done does not make sense to you, look to outside resources in order to inform you as to why things are done in a certain way. Within the pages of this book, my goal is to give you a pretty good understanding of why and how things are done in Java; however since this is a beginners guide, there are certainly a lack of more intermediate and advanced options and descriptions that you may be ready for in a short period of time. In order to learn how to speak Java to a computer in order to program it, first a somewhat in depth idea of what computer programming is invaluable for a new programmer in training. The basic activities of computer programming are developing an understanding of programming itself, how to generate algorithms, ability to understand the computer analysis, idea of how to verify that your algorithms are correct and finally to implement (or code) your desired algorithms, in this case, Java. First here are some key terms that are also helpful to get used to using while developing your computer programming language skills. Common Helpful Programming Terms and Definitions Algorithm- a way in which to use logically or mathematical skills in order to solve problems API- stands for application programming interface. It is essentially a set of rules that allow code to communicate with other websites or software Code- also known as source code. It is a collection of computer instructions written using a programming language
Page
6
Code Editor- a type of text editor program specifically designed for computer programmers. Can be used as an application or can be integrated into software or a web browser. You essentially are able to check the code you have written, like a traditional copy editor in a newspaper or magazine, just computerized! Complier- This is a program that allows for you to take code written by you and is translated into what is known as binary (essentially ones and zeros that make up the actual machine code) Concatenation- this is the ability to combine strings of text together (things like two lists) Control Flow- this is the computers control over the order that sections of coding are run. Essentially it is like rules of how the flow of data moves Dump- this is a list of data or something like a text file that is saved in the event of your program crashing. It can be very helpful in diagnosing problems (why the crash occurred) Floating point- basically it is a decimal number where the decimal itself is referred to as the floating point (ex 4.56) Function- written instructions that obtain a particular result with the ability to be used simply by writing them or “calling” them GIT- this is a portion of distributed software used for revision control that is used as a source code management system (also referred to by the acronym SCM) GUI- this acronym stands for General User Interface referring to the software you are able to see and interact with (the icons or programs you would generally think of i.e. internet browser) IDE- this is an acronym that stands for integrated development environment. It is slightly more advance than your basic code editor Interpreter- this is something that allows for translations of the computer coding (machine code) when a program is in use Iteration- instructions that are repeated JSON- this is a format used to transmit information to and from locations Libraries- pre written code that is used to implement common coding features which you do not have to write yourself Logical Operation- using Boolean logic (and, or and not are the key words when using this type of operative computer coding) Loop- the use of a piece of code that runs until a condition placed on it is filled Markup Language- a language format, like HTML, that is relatively simple and used to format pages Nested- an instance where one thing is contained inside of another
Page
7
Pseudo code- general description of the pathway between code and written language. This is code written in English that allows for a message to be sent to tell a given piece of code what to do Recursion- this occurs when something actually refers to itself Run time- this is the time that a program is in use, or running Sandbox- testing ground in which to run a program test SDK- an acronym that stands for software development kit. SDK is a bundle of software tools that allow you to create new applications for specific framework or platforms Subroutine- a portion of code or a function that can be run anywhere inside of a program Syntax- the set of rules in a computer language that guide usage or correct structure Variable- this is data stored that has the ability to be modified by the user whenever they would like to Now that you have a small dictionary of terms to help you understand key concepts, I will now provide more thorough descriptions of how and what programming works and what it is. These next few paragraphs will give you an excellent foundation of knowledge for what and how to program. This beginner’s guide to understanding will have you set on a proper knowledge and explanation of basic programming principles. What is Programming? Computer programming is defined as a process where one takes a computing problem and turns it into executable computer programs. The end goal of programming is to be able to discover instructions in a given programming language that will allow for solving a problem or automate performing a specific task. In order to fully understand how to execute this process, one should have good analytic skills, an understanding of how a computer works, be able to understand and generate algorithms and finally implement (or code) algorithms of a given programming language. Keep in mind that there is more than one language, so the world of programming is much, much larger than the scope of this book. In addition to these basic principles, I want to reiterate how learning computer programming code is really laying the foundation for either the system itself or a program to be used in it. When you are programming you need to be debugging and testing while maintain the source code. Essentially, once you start doing coding for yourself of a program or system, you need to be able to problem solve and diffuse the issues with code quickly. This book should serve as an excellent beginner’s guide and give you skills you may need to trouble shoot issues you may have while starting the process. Also with programming and coding you are learning how to build and implement the system. These terms are often used interchangeably to describe programming of the
Page
8
computer itself, creating source code or implementing said programming or coding. Once you get more familiar with what programming and coding can do you will learn how they really work together in order to create the virtual environment, buy also be able to identify the subtle differences. How to Generate Algorithms Algorithms are important because they allow for us to do things, in the computer world, like schedule tasks, design computer algorithms and even solve other optimization issues. Essentially, they are something a computer does to instruct it to complete a task in an efficient manner. Sometimes people even refer to them as recipes because they are similar in the structure and the fact that if you find a good way to carry out a certain task, it can be very rewarding (much like warm chocolate chip cookies are from a traditional recipe). As for you and creating your own algorithms, this can be a complex task for any computer developer, even more so when you are first starting out. The good news is that you probably will not be dealing with high level algorithms for a long time. Those algorithms that you will be using are things like creating everyday functions or writing loops. Essentially, you are writing an algorithm to complete a task for you. There is a simple test list for creating an efficient algorithm design: You want to use language features that enable you to reduce operations You want to reduce iterative loop nesting as best as you can You want to have the ability to define variables outside of loops when it is possible Try to use automatic loop indexing instead of manually doing so Try to use clever reduction techniques (things like conquer and query optimization) in an effort to minimize the size of recursive processes Basically, when you are creating or using an algorithm, you want to settle on something that not only completes a task and solves a problem—you also want this to be done in the most efficient way possible. One way to gain insight to what is efficient is to look at the work that has already been done by others. Within this book, I will not be able to give you a complete understanding of algorithms but I most certainly want to give you a good place to start. I am going to give you an examples and, from here, I hope that you will have a good jumping off point for your future projects. This example is a very simple and somewhat standard example of an algorithm. It is often referred to as the “Hello World” program and the following will be an example of it. In the next chapters, I will go over how to use and run Java, this is just an example of something you may want to input and try (beginner algorithm). Sample Algorithm public class HelloWorld
Page
9
{ public static void main (String[ ] args) { System.out.println(“Helloworld!”) ; } } That is a very basic example of an algorithm you may use to start to understand how your programming skills can be used. If ever you are trying to get an algorithm to function and not having any luck, check something simple like the spacing or wrong character or symbol—it usually boils down to a simple error. Understanding Computer Analysis As the word analysis suggests, computer analysis consists of looking and troubleshooting for problems or software issues within your system or programs. Essentially, being able to understand how a computer works is a key component to be able to isolate any issue and solve it. This part of the programming is something you should have a good firm grasp on, as with any new task to learn, it is good to be very familiar with how and why your hardware or tools operate. This is the same with computer analysis. Keeping up with the newest technologies, programs and incorporating how and why you should use a method or program is important to know. Picking up this book and reading it is a great start to learning how coding and programming works. After you have mastered Java, do not fear! There are many different languages and systems to explore and master as well. The greatest thing about technology is that it is constantly being innovated into the best possible thing it can be. Knowing how to secure systems and resolve issues will get you very far in many aspects of life, particularly, when sensitive issues or data may be at stake, eventually something you will deal with as a programmer. How to Verify you Code is Correct One sure way to verify if your code is correct is to try and run it. However, if the code is something you are doing outside of your sandbox, you have several other methodologies to use to ensure you are getting your correct message across. There are many different online websites, serving as code editors that you can utilize in order to check code. These are websites such as Tinkerbin, Write Code Online or Codepad. Basically, if you do a google search for a code editing program for any language you are using, you will be able to find a suitable interface to do so. Having a sandbox to play around with your coding is a very efficient way in which to check your code. By utilizing this method you are able to create a virtual environment to test out code to make sure it will complete the proper function. This is important if you are going to code something that will effect the ability of a program, software or even website that you are attempting to build.
Page
10
Another great resource you can use to help insure you have proper code is something called a library. As you are probably familiar with a library of books, this is similar as it is a large virtual library that contains code. Using this method can be great if you need to make sure something is up to par and working. However, I would caution that you always be able to understand the code you are using—even check that code with a code editor. You should be able to understand the code in order to fix it if a problem arises. If you just copy and paste from a library, without understanding, it could lead to huge potential issues down the road. Now that you have a great, general foundation for what programming is, let us move on to Java specifically. I will lay out in the next few chapters some important understand of what and how to use Java and its functions. This will all build on the information in this chapter, so hopefully you have a fairly firm understanding of some of the main parts of the world of coding and programming.
Page
11
Chapter 2- What is Java? Now that you have a better idea of what programming is, let us move into learning about exact what Java is and how it fits into the world of coding and programming. As a computer programming language, which is what Java is, it is designed to be a language that it class based, object oriented, concurrent and has the least implementation dependencies as possible. Essentially, any platform that supports Java can run Java without a need for recompilation. This is one of the reasons that Java is one of the most popular languages in use today. One phrase that developers use to describe the multi-platform use of Java is “Write once, run anywhere” also known by the acronym WORA. As a language, you may have a learning curve when it comes to the understanding of why and how certain things are done within the language. Typically within this book, I try to give as many explanations to you as possible without getting into unnecessary detail for you at the beginner level. With that being said, as with the English language, there will probably be rules and situations you do not fully understand why they are formatted or done in a certain way. Rather than spending time and effort into understanding these things, I encourage you to just accept them as they are. Although you may want a better answer than that, unfortunately I think only the creators themselves know the reasoning behind them—and it may even just boil down to their preference or the computers ability to understand. Java was created by a man named James Gosling in 1995. He designed Java as a programming language that has fewer low level facilities than languages like C and C++. Essentially meaning that it focuses on the high level, essentials for running computer programs rather than the more minute and micro details that these other programming languages have. There are five main goals that Gosling used to guide him in the creation of Java, These are that 1) it must be threaded, interpreted and dynamic, 2) it must be object oriented, simple and familiar, 3) it must be secure and robust, 4) it must execute with high performance and 5) it must be portable and architecture- neutral. These five goals explain why it is such a commonly used language. These are goals that allow for users to have a mainstream, useable and effective method for building anything from software to a website. User friendly programs are something that bring the complexities of computer programming into a more relatable experience, especially for new users and those just starting out learning. This is why I personally recommend and find it important for beginners to have a firm grasp on. It is very widely used and therefore pretty important to know and understand for anyone trying to work, or even just have as a hobby, computer programming and coding. Now that there is a little background on Java, how do you get the actual program running on your own system? Basically, one of the more difficult things to start coding Java is acquiring all of the necessary tools to run it. Java is an independent platform, which means it will run on just about any operating system. The reason for this is a program called Java Virtual Machine (also called Java Runtime Environment). In order to start coding in Java, you need to download this program in order to do so. You can actually make sure you have this program, as some computers do have it installed, or install it through the Java website. There are options for many operating systems so
Page
12
you should be good to go once you are able to locate it and download it! So now you have downloaded (or discovered you had) this program—what does it mean? It means that you can run Java on your computer, we will get to the actual program to download the coding now. That is known as Java’s Software Development Kit, it can be downloaded off the Oracle website (it is the company that owns Java). The one you want to use it called Java SE (Standard Edition). For the purposes of being able to code, we are going to do so using something called NetBeans, so once you find Java SE you will want to find an option that is called JDK 8 with NetBeans. You are also going to want to go to the NetBeans website and download software there. After you have gained access to the software, we are ready to code! Essentially once you have all the correct software, you are going to begin to write your code in a text editor. If you choose a different option than NetBeans, you will find the place where you write the source code saved under the file extension .java. There is a program known as Javac that then turns source code into Java Byte Code. This process is called compiling, just FYI. After Javac has completed its task, it creates a new file with the extension of .class instead of .java. That is, if no errors are detected in your code—otherwise you will have to go back and find that probably small error you made and fix it. Finally, at this point, you can run your code on the Java Virtual Machine! In case it is more helpful for you, here are the three basic steps you will use to get your code running—first is to create your source code with the .java extension, then you will use Javac to create a file ending in .class and third you run the complied class. Now that you have a greater understanding of how you will create and complete actual coding within Java, you should be able to begin the process of creating your own codes! Within the suggested NetBeans software, it is a great environment to teach yourself some of the ins and outs of Java. This chapter really serves as a basic description of the software and a very general overview of a way to download and begin to use it. Throughout the remainder of this book, I will talk more about some of the technicalities that help you understand how and why you must do certain things in Java (I.E. learn to speak the language fairly fluently, with the knowledge of how things work to back up your fluency of language).
Page
13
Chapter 3- Conditions and Control Flow In order to properly be able to utilize Java, it is important to understand the rules of the language, like grammar and spelling in the English language. In order to properly tell the computer what to do, we need to understand what the terms conditions and control flow really mean within the coding and programming world. For conditional statements, we can think of these as if/then statements that are used to tell the computer different actions to perform or computations to make. As for control flow, it is the order in which instructions, statements or function calls of a program are executed or evaluated. You can think of these as sort of the rules for sentence structure and the usage of a common, semi colon or other literary symbolically represented rule. These are very important in programming because computers, unlike humans, do not have deductive reasoning skills. They carry out the functions exactly as they are told to do so. Both the conditions and control flow work together in order to create the proper environmental flow of data. Without either of these concepts, it would be difficult to have Java work in the user friendly manner that it does. A key component of a computer is that it is reliable as well as straightforward without multiple interpretations of the same thing. Computers do not read minds, they execute tasks as their function. Conditions If you have taken an English class in high school or college, you probably have heard the term conditional statement within that context. Although this is obviously a different context than an English classroom, the basic functionality is roughly the same. There are two different types of programming languages, functional and imperative, Java falls into the imperative category so we will be covering the specific conditions of this type of programming. When we look at imperative programming languages, we see the term “conditional statement” typically used. These essentially are structures having one or more conditions that need to be executed if the statement or statements have determined to be true, some are also executed if they are false, depending on the statement. In Java we see four types of statements that are made. These four statements are if/else statements, if statements, switch statements or finally nested if statements. There are if statements that are a Boolean expression (Boolean is when there are two outcomes, like true or false). Whereas if/else statement is occurs when and if statement is followed by an else statement when it is executed in the code it will return the answer of false. A nested if statement is something that is two statements with one “nestled” inside of the other; these are either if or else statements. Finally a switch statement is one that takes a variable and tests it against a list of given variables. These statements are all made up of questions that a computer can process and execute functions that are, at each of their essences, made up of Boolean (or two outcome) expressions—except for switch statements which compare one variable against a list (for example, what is the greatest number (maximum number) in a given list. Although multiple statements may be wrapped inside of another with the if statement format of any kind, it is important to remember that at their core
Page
14
they are asking simple two answer questions. As I stated before, computers are incapable of inference and this really drives this point to home, these questions demand either of two responses, or it will create the third option of an error. Next we will address more about control flow and how these statements are executed in the programming. It will also have some overlap with these conditional statements, as they are both methods of communicating (coding) what the computer should do and how it should do it. Control Flow As I stated earlier, control flow is the manner in which a given program executes or evaluates statements. As with any language, there are a lot of rules you have to follow in order to communicate properly to another person (in this case, machine). It is important to understand the reasoning behind these things in order to properly speak the language of the computer. Learning about control flow, even if we are specifically talking about Java, can easily be applied to other languages, although specifics may not be exactly the same. This is another part of building the foundation for your programming and coding future so it is important to make sure you understand it as well as possible. Control flow statements are different in each different programming language, however they can be categorized by the effect they have. There are five unique categories that these statements can fall into. These five statements are 1) a set of statements executed only if a condition is met, 2) continuation of execution at a different statement, a set of statements executed zero or more times until a specific condition is met, a set of distance statements after they are executed the flow will return and finally 5) stopping of the program, this will prevent any further execution. Now in Java, when we are dealing with something like a common loop control statement; specifically we are looking at the change a loop statement makes from its normal sequence. Java specifically supports two different types of control statements in this scenario, break statements and continue statements. Break statements are those that terminate the loop (also known as the aforementioned switch statement I discussed above) in which the actually execution moves on to whatever immediately follows the loop/switch. A continue statement is one that tells the program to essentially skip any code following it and retest the condition. Essentially when we look at both conditions and control flow we see things that tell the computer what it should or should not to. Through programming we are able to give the computer specific instructions on how and why it should execute statements. Although these are really very brief definitions and descriptions of what and how both of these mechanisms operate, they give you an excellent foundation for knowing how they work. Utilizing the use of statements like these is important work to figure out while you are learning Java. The benefits of Java are that these are routine functions and there is a lot of trial and error that can be easily done when you begin to experiment with coding and programming. These are important and key features you should certainly feel comfortable with before moving on to anything more advanced. At each step of your programming journey, make sure you are practicing and making sure you are firm in your ability and knowledge of what, how and why you are doing any procedure.
Page
15
(This page has no text content)
Page
16
Chapter 4- Object Oriented Java In this type of an object oriented language, which is what Java can be described as, you are looking at a program paradigm that is really based on the concept of “objects”. These objects can contain data, in the form of fields (often called attributes) and code that is in the form of procedures (often referred to as methods). One of the features of an object is that the object’s procedures have the ability to access and often modify the data fields of a given object they are associated with. As Java is an object oriented language, there are fundamental concepts that it utilizes in order to fall into this class of language. The concepts of classes and objects are of particular importance and what we will discuss and analyze in this chapter. In terms of Java, an object has both states and behaviors. When we talk about class in this programming context, we are looking at the outline that allows for a description of the state or behavior that the object supports. Essentially, class is what you use to define what the object is. Now let’s get a little clearer understanding of what each of these things are, starting with objects. When we think about “real world” objects, we probably think about things like dogs, other people or cars. Each of these objects we think of does have a state as well as a behavior, just as it does within Java. If we want to look at a human, for example, they have different states (names, skin colors, and ethnicity) and they also have behavior (speaking, working or sleeping). Each of these states and behaviors can be thought of as different parts of the same person. In fact, every person has a different set of behaviors and states, just as with most objects we interact with on a daily basis. In comparison with real world objects, there can be very similar characteristics in some instances. The state of software object is stored within the fields and behavior is shown in the methods. So basically, methods are internally operating both the singular object and allowing the communication to happen via a method between objects. Just as with the human example, we have internal communication happening within ourselves and we use these internally stored methods to gain access to other objects (whether that be a car, computer or another human). So what about class? It can be defined as the blueprint that individual objects are created from. There are three different types of variables that any class can contain. These variables are local variables, instance variables and class variables. Local variables are those that are localized, or within your method. They are initialized and declared locally—or within the method. When the method is completed, the variable will them be destroyed. Instance variables are those that inside of a class but actually are outside of methods. They occur when the class itself is instantiated. The instance variables have the ability to be accessed from any blocks or constructors of the class they are a part of. The last variable type, class variables, are those that, as the name suggests, are within the class. They operate outside of any method however they can use methods in order to access values. What do we do if we would like to create an object from a class itself? There are actually three
Page
17
different steps that will allow us to do just that. These steps are declaration, initialization and instantiation. A variable declaration is when a variable has both an object type with a name for the variable. An instantiation is the new keyword that you would use to create a given object. Finally initialization is what is used to call a given constructor and has the ability to initialize the new object. For example, you could create a person (object) in the human class and then name the puppy Spot, giving name to the constructor and creating a new object. Let me give you a little more clarification on constructors in the next paragraph. When we look more into classes, we have to also talk about constructors. Constructors are a bit of code allowing you to create a given object from a class. Constructors are a necessity in every class. If you do not include one, the Java complier actually creates a default constructor for the class at hand. You can always be certain that if you create an object, one or more constructors will be invoked for that object. Technically constructors should really have a name that is the same as the class they are in; however, classes can have more than one given constructor. For example, if you had a class known as puppy, the constructors could be lab or bulldog—or like the example in the previous paragraph of a name like Spot. Now let us talk a little bit about some rules that are needed to be followed when you are declaring classes. Source File Declaration Rules These source file declaration rules are actually essential when you are doing things with your different source file statements or import statements. They are also important when you are declaring given classes (which is a Java class file with the .class extension). You can only have one public class per source file The package statement should always appear first inside of a package One source file is able to have multiple nonpublic classes When dealing with import statements, you always need to remember that statements are written sandwiched between both the class declaration and the package statement. If you do not have any package statement the very first line in the given source file should be the import statement If you have both source files and public class files you should have them both be the same name. They should also be appended by .java at the end of the file. Example- administrator.java When you have both import and package statements, the classes they are associate with will be applicable fully in any source file that you are using. You cannot have source files that contain package and import statements associated with different classes within the source file
Page
18
Chapter 5- Data Structures Within Java there are certain types of data that are used and defined by the program. I am sure that, in some form or another, you have heard of data—it could be the gigabytes of megabytes of data used by your cell phone or maybe you have a backup for your computer that stores data in terabytes. Each of these different terms tells you how much data your device or backup can support and use. When it comes to data itself, there are many different ways we use this terminology both inside the virtual world and in the real world. When we look at data, I personally think of it as something that gives me quantifiable means to look into or solve a problem or question I may have. Within the world of Java, data is just defined in its own computer programming and coding language manner. In terms of the type of data Java can support, it is much vaster than just the typical “byte” data you have heard in the past. Java actually can support eight different primitive data types. These data types are byte, short, integer (referred to as int), long, float, double, Boolean and char. Now that you have heard these types of data, let us break them down and tell you what they mean and what they can do for a user. Byte A byte is what can be considered a unit of memory size that is actually a group of bits, or binary units, typically eight that is operated on as a unit. What does this mean for your usage of Java? Well, within Java it has a minimum value or -128 and a maximum, inclusive value of 127, as well as has a default value of zero. This type of byte data is actually used in Java to save space in large arrays—most of the time in the place of integers as a byte is actually four times smaller than an integer. Typically you will see byte represented in a similar fashion to this—ex -100 byte. Short Where byte data requires full integers, you can use short data to contain integer values that do not actually need or require the entire width of integer. Short data can, in some cases, pack your short variables closely together in an effort to save memory consumption. Within Java, short data type is 16 bit data with a minimum values of -32.768 or maximum value of 32,767 also with a default value of 0. These amounts are actually two simple equations—(-2^15 and 2^15 -1 respectively). This type of data is also good for saving memory as a byte type data we spoke about in the previous byte section. That being said, short data can be used in an effort to save memory as the byte type data as it is two times smaller than an integer. A couple of examples of how you may see short data are short s= 20000 or short r = -100000. Int This type of data, in long, unabbreviated form, is actually referred to ask integer data. The definition of integer data is that it is a very small subset of mathematical integers that can be seen as a group of what is known as binary digits (often referred to as bits). Computer hardware
Page
19
always wants to have a way for the user to represent data (like memory address or processor regester) as an integer In terms of its uses in Java, integer or int data is a 32 bit signed two’s complement integer with a minimum value of -2,147,483,648 and a maximum, inclusive value of 2,147,483,647 and has a default value of 0.These can be represented by the equations -2^31 and 2^31 -1 respectively. Integer data is the most commonly used data type for integer values, in fact it is often used as the default type of data. If there is a concern about memory, however, you may not want to use this method (as you can see the minimums and maximums are substantially larger than the previous two types of data). You will also see this data represented in a format such as int a= 200000 and int b = -100000. Long As you would gather from its namesake, long data is in fact very long in the minimum and maximum values it utilizes. The minimum data value for long data is -9,223,372,036,854,775,808 and the maximum is 9,223,372,036,854,775,807 and is inclusive with a default value of 0L. These are represented by the equations -2^63 and 2^63 -1. Long data is used when a much wider range than integer data is need. You will see long form data like these examples—long a = 200000L or long b=-100000L. Float With Float data we are looking at a single prevision 32 bit IEEE 754 floating point. When you want to save memory, you may want to look into this option to be able to save larger data amounts. The default value for this data is 0.0f and this type of data is never used for values that are considered precise, such as currency. You will see it represents like float f1= 345.6f Double Double data is a type of double precision, 64 but IEEE 754 floating point data. This type of data is the default used for storing decimal values. Always remember that these types of data should not be used when you are dealing with precise values, or whole integers. We have a default value of 0.0d for this type of data, example—double d1= 567.8. Boolean We talked about Boolean data briefly before. Boolean data is the type of data that only has two different outcomes, true or false. It is used in order to find and track both true and false conditions. Always remember that, unlike you may be inclined to think, the actual default value in this data set, rather than some form of zero, is false. This makes sense when you look at true and false as the only possible outcomes. You would see this type of data represented in an example like Boolean one =true. Char This char, or character, type of data is a single, 16 bit Unicode character. This data is represented by a minimum value of ‘/u0000 (or 0) and the maximum value is ‘/uffff (or 65,535) and the maximum value is inclusive. This type of data is used to store any character. An example of how you would see this data is char letterB =”B”
Page
20
Now at the beginning of talking about data, I wrote that these are primitive types of data. Something that may be helpful to you when thinking of data is referring to our previous content above about classes that is actually non primitive data. However, they are both ways of representing something in our coding and programming that are ways in which we categorize and store materials—or data. Data in any sense is something that we gather and organize in order to properly explain or store for future use. Although these various types of data may seem somewhat obscure by just reading these definitions, once you start experimenting and seeing how various algorithms or coding is developed in your own usage, you will find reasons for each of their uses. Reference Data Within the world of Java data, we will also see what is referred to as reference datatypes. We can view reference variables as those that are created when actually utilizing constructors of the given class or classes to define them. These types of data are used as a method in which to actually access objects. Reference variables are those that are not able to be changed as a point of definiton (for example—puppy or employee). There are some variables and class objects that actually have a reference variable without value (or null). Using these you can refer to objects that can be compared with each other. An example of this is Animal = new animal(“cat”). Any primitive type data can be seen as a literal. Source code that is literal is something that has a fixed value (or unchangeable). Basically, you cannot have anything within the value other than the code, having no computation. These literals can be assigned to any primitive type of variable, example char d = “D”. When you think of literal, it somewhat means a taking at word at the base sense (most literal value). There are also what is called String and char literals that support few special escape sequences that are good to note. These are things like /n, which means newline, /b or backspace, /s or space, // which is backlash, /t or tab, /’ which is single quote, /” or double quote, /r or carriage return, /f or formfeed and finally /ddd or octal character. These special escape sequences may come in handy for you in the future. Take reference of them for now and there will be times in your programming that they may be useful. The beginning of this chapter really helped to explain data types, now we are going to switch gears and talk about variables. Variables As with in any math based activity, you can manipulate the variables in Java in order to name specific variables in a helpful way. Each variable does have different sets of “rules” that are associated with it that can be applied to the variable at hand. Within Java, you must declare a variable before it is used. There are the three different types of variables we can use in order to give proper categories of organization for the variables themselves. In the previous chapter, we discussed variables in some detail. The reason for addition an additional section in this chapter is to bring to your attention how they are related to the data and data structures. It is important to remember that there are other forms of data than the memory we assign to them to let us know how much room we need for them.
Comments 0
Loading comments...
Reply to Comment
Edit Comment