Creating Your First App with Java
Key Takeaways
This video tutorial covers creating a simple calculator application in Java, introducing essential concepts such as Java primitives, Strings, arithmetic operations, and control flow.
Full Transcript
hi there data scamps and data champs this is Richie so Java has been one of the most popular programming languages for software development for decades now and I feel like knowing some Java is an essential skill uh particularly if you're in Enterprise software development and in the data realm because it's got a lot of math and machine learning capabilities it's also used for scientific software and is very popular for data engineering as well uh but today we're going to make something simpler so this session is designed for complete beginners to Java so that means we're going to make a simple calculator just you get a feel for the syntax and complete some sort of technical problems Our Guest is Jim White he's an information technology professional uh with over 35 years of software engineering experience so uh before we went live we were just chatting about how Java is going to be 30 years old later this year and Jim remembered when it was first released so he's been there he's been here since the beginning now uh Jim specializes in Internet of things and Edge systems uh also Enterprise application integration Solutions and mobile applications and among his many accomplishments he co-founded and led The edex Foundry which is uh uh an iot uh company and he co-authored uh Java 2 micro Edition Java in small things he's also the instructor of the data Camp uh introduction to Java course so uh please take it away Jim hey good afternoon good morning good evening wherever you are it's a pleasure to be with you all today um hope you're having a good day and thanks to Diddy Camp thanks to Rees and Richie for um for hosting us today here on Diddy Camp's webinar um want to kind of get you into things here quickly but first before we do so let's do a couple of uh a couple of checks uh first of all I'm gonna assume you have some interest in learning programming and in particular Java today as Richie mentioned so my goal is to try teach you enough about Java that you're excited and you want to learn more maybe my second goal is to get you to a point where you're like where do I go and find more so I'll try and point you to some resources in order to be able to take that next step and of course one of those resources we're going to try to point you to is data cams Java courses as Richie mentioned I'm the author of one of the courses and U we'll chat about that towards the end um please let's try to make this as interactive as we can today so if you have a question as we're going along drop it into the end of the chat Rees and Richie are keeping me honest here and trying to keep me informed of your questions as they come in we'll try and answer them but as Reese mentioned we'll also have a a period at the end where we'll cover questions as well couple other small items before we get started um you might be wondering hey can I get the slides uh for what you're covering here today Jim or can I get some of the background information can I get the code that we're going to be seeing today in our codal along all of this information is available online if you go to this bit.ly address bit.ly data Camp Java intro um you will find all the materials in fact I can uh I can show you right now what that looks like you'll come to a little bit of a dropbox location where the slides again the code uh that we'll ultimately have but also the steps to get to the code that we'll ultimately have including a a a a PDF document out there about how to get the Community Edition set up if you haven't done so already so all that available there online for you um scan the QR code now if you'd like to have a quick shortcut to that material set what else you're going to need um jetbrains intell J IDE it's Community Edition there are a couple of different versions that um intell J comes out with one is a um for pay uh IDE and another is a completely free Community Edition uh which is what we're going to use today to do some of our Java coding so if you haven't done so yet and you want to actually do the Cod along as we're practicing it here online in this webinar today go to this site now and download that particular Community Edition that meets your particular browser I'm sorry um desktop so if you go to um if you go to Jet brain site you'll find a page like this where you will be able to download the Community Edition for your particular operating system and Hardware of choice again keep to the Community Edition you can go out and get the ultimate edition But ultimately then you're going to ask be asked by intellig and Jeet brains to pay a fee if you want to continue to use that IDE so we we suggest at least for the period of this course today look at the community Edition all right uh let's see what else I think that pretty much covers um what uh what we need to get started here this morning again thanks Richie for the the great introduction as you mentioned I've been around the Java Community for quite a while yes I actually do remember when it first came out which tells you that I have a few gray hairs named after my favorite uh projects out there um and as Richie mentioned I've also been an author uh co-founded an open source project called adx Foundry which is a an Internet of Things framework uh frequent speaker and author and then now I spend uh some of my time in retirement I am semi-retired which means I also get to do things like this which is to author things like data Camp courses U teach online and do a little bit of Consulting on the side so try to try to stay active in the community out there and again just really looking forward to presenting Java to you all today and get you started on your programming journey in Java okay so with that couple of uh points too about Java just to kind of give you a level set what we're talking about here in terms of a programming language Richie mentioned it's uh it's its 30th birthday here I think it's this month um it is a general purpose programming language it's used for cloud development uh to build applications on servers that might be in your companies uh web applications desktop applications mobile applications you name it Java has been there done that in fact one of the mantras of of java is Right want run anywhere and that's why it's been become such a popular programming language out there it is pervasive um you find it in all sorts of Industries Finance Health manufacturer are three big ones that you're going to find it in out there there's an estimated I think I read uh last night somewhere there's an estimated number of developers in the world at about 30 million of 30 million um about 9 million are Java developers so that gives you a pretty good indication of how big Java is about a third of the world's development Community uses Java in some capacity again it's used by big companies um in particular Fortune 500 it's about 90% of those companies have some Java in their environment and you'll also find it in three million mobile devices out there in particular Android devices is usually full of java applications and um Google uses Java quite a bit in the platform itself you will also find it in some pretty ex exciting environment uh it was used in the Mars rover so it's on Mars uh for for those of you who were Gamers it was used in one of the first popular or maybe semi- poopular games out there Minecraft uh it's used in things like Wikipedia search and you'll find it in lots and lots of other applications to include you'll find it uh being used extensively in places like Netflix Spotify and even X or what used to be Twitter so Java is a pretty popular uh language out there it's been used for many many years in many different types of environments so of course that's one of the reasons you want to Java is because it is very popular but what are some of the other reasons you're going to find that again it's a very mature language um and it's pretty straightforward to learn in fact a lot of the languages what we see out there in our world today have taken lessons from and pieces from the Java language and made it part of um what is is in their languages today so it's a great place to start learning to program and of course because it is so popular it's not going to be a forgotten language you're going to use it but you also find a lot of threads in Java out there in a lot of other environments you're going to come across if you begin to program more there is a huge Community providing an endless supply of resources uh for developers at any level um and I'm going to point you to a couple of those here as we go along today uh especially for those of you who are beginners some places where you can learn Java at uh what I call a nice gentle easy relaxing way to do so so Java is one of those languages where it's not hard to find some help if you do run into situations where you're a beginner and looking to try and get into the language runs on any hardware architecture uh runs on Intel platforms runs on arm um you name it it's been there and done that it runs on all operating systems Windows uh Mac OS Linux Unix again you're going to find Java is a language it believes strongly in being able to run just about any place you find out there that Mantra of write once Run Anywhere is truly one of its strengths and one of the reasons why many people use and go to Java um you will find it's pretty robust and secure as well as open source what do those mean well robust obviously it's been around quite a bit so uh a lot of the bugs if you will have been worked out of the language um and it is secure uh when I talk about secure what I mean by that too is not only is it secure from a standpoint of making sure that you know we don't have intrusive Behavior into our applications it's also secure in that many of you may have experienced what we call the blue screen of death on your windows platform um or you've had an operating system crash on you what's the reason for that typically it's because an application or even the operating system has started to use a piece of memory on your system that was supposed to be used by another application or by the operating system and when those two start to collide um we run into security issues there where applications are using each other's memory a Java prevents that it actually physically has a way to make sure that doesn't happen happen by using something called a virtual machine we won't be able to get into a lot of the details of the virtual machine and and the reasons why that is but just know that Java is one of those environments that is robust and secure uh because of that virtual machine apparatus and of course when we say open source what do we mean by that a lot of people associate that with free uh it is free uh from the perspective of being able to get in learn it start to use it uh depending on how your your company deploys your applications you may have to provide some sort of Licensing fee to the ultimate provider of java but from the entry level and getting in and starting to learn language it is completely open source and we'll see and use that today there are also plenty of java libraries out there for those not familiar library is a term we use for packages of already built code that you can use and get from third parties or or others inside of your organization and there are just oodles and oodles of these Java libraries that exist out there today again because it's a mature language it's had many years to develop these kinds of libraries you'll find libraries to help you do everything from getting um cryptographic types of uh apparatus set up to working with AI and ML and bringing in data from all sorts of environments if you name a particular circumstance whereby you need some help probably you're going to find there's a Java library that already exists to get you well on your way there you may have to provide a little bit of code on your own but in many cases at least in all the applications I've ever written there's already a good head start by the number of java libraries that exist out there so that's some of the reasons why um a lot of people are out there looking at or starting to work with or have Java in their environments so we've been talking already here for a few minutes enough talk Jim let's start to code what are we going to learn today in our coding exercise here we're going to learn the programming structure of java we're going to learn how to comment uh always critical to be able to put in there notes to yourself about what's going on in your programs we're going to learn how to set up what we call a variable and a assign some data to those variables we'll talk about what a variable is we'll talk about the different types of data that we have in Java namely Primitives and strings and objects we'll do some simple terminal input and output meaning we're going to be able to get data from the user and be able to display data back to the user we'll also talk about control flow things like if then and switches and Loops um the mechanisms by which we determine how uh lines of code get executed inside of a program and then if time permits here we'll get into a little bit of exception handling one of java strengths is its ability to deal with problems that happen in almost any type of programming environment and exception handling is jvas framework for doing so so without further Ado let's go ahead then and pop into um intellig so this is intell's Community Edition if you have yours available and you want to code along and bring it up maybe on a separate screen I encourage you to do so the very first thing that we're going to do is we're going to open up a new project so inside of intellig here you'll notice there's a button at least in my particular environment it's going to be up in the right hand corner here it says open new project so I'm going to do that and wouldn't you know it it's going to display a screen on another screen here so let me pull that up and what I'd like you to do in the new project dialogue box that comes up is create a project we're going to call ours uh Java calculator you can call it whatever you'd like this is just a mechanism for which intellig is going to organize things the idea of a project is if you will just kind of a folder where it stuffs all sorts of things including your source code and any other elements part of your application we're going to keep things pretty simple today we're really going to keep a a project just uh with one particular file in it but we'll create a project called Java calculator the location it doesn't really Mir just put it someplace where you can go find it again if you want to again work onun uh this project later on all the other things you can leave in their default situation one thing I will highlight here just so that you're aware when you install intellig um you also install Java or what's known as the Java developer kit and so when you take a look at the project getting created ask you which Java you want to use and it's using the um already uh provided uh jdk or Java environment that was installed When intell J started so you don't have to install Java and then install intell it does that all for you so a nice way to kind of get started here really really quickly is just install intell's Community Edition create a project and we'll be coding in no time so calling my project Java calculator I'm going to go ahead and hit the create button it's going to go ahead and put together now um a project uh and me bring that up here when it creates this project it also creates bya fa a class now we're going to talk about what a class here is in just a second but you'll see this class Main and it actually puts it in a file called main that's the default way that intell works it says well you must be ready to start some coding let's put give you the the apparatus by which you start some coding by that I mean creating this class where you could start to drop in code we're going to not use intell's class me we're going to actually create our own right from scratch so you know how a Java application is built right right from the get-go without any bells or whistles or extra stuff being provided by a third party here so what I'm going to ask you to do is actually delete that file so you can go to the uh the project Explorer here which is the little window on the left hand side right click on Main and just ask to delete that particular file we don't we don't need main so we'll go ahead and say yep go ahead and delete that so now we have an empty project called Java cal calculator we have this little folder called SRC that stands for Source where your source code is going to go and from there that's where we'll get started in our beginning Java entry here so what do we need to start coding Java in Java everything is placed into something called a class there are other beginning points which will be beyond our class day there are other types of things that you might find are defined in a Java application those are a little bit more advanced but for 90% of what we do in any kind of in in situation in in Java development we create and use class as a way to organize our code so the very first thing I'm going to do is again right click on source and ask to develop a new Java class and it says well I'd like a name for that particular class let's just call it calculator again it doesn't really matter what you want to call it you could call it fubar you call it George it doesn't really matter but since we're building a calculator it seems to make sense that we'll call this class calculator and I'll say yep let's go ahead and create that inell J okay now once we've done that let me see if I can zoom in here a little bit because I think it's h maybe a little tiny let's see re let me know if if that needs to be a little bit bigger otherwise I'm going to assume that we're doing pretty good there but if things are a little bit tiny Reese type me out a message and let me know um so we've got this public class calculator and then you see two little brackets what we call euphemistically in Java squiggly brackets or left and right bracket so everything in Java all code has to go into a class the word public what does that mean going to be a little bit beyond scope of our class is uh is this idea of uh public today but as you might assume there's going to be a public and private um the idea being that which everybody can see is public and that which maybe some people can see and others can't might be private we're going to leave that for maybe kind kind of part of your future journey into uh into Java but from the perspective of just getting us going you need to have a class to start to put code in yep it's going to be public by default as you see here which intelligate does and again it has to have a name in this case calculator good place to also mention or look at the fact that our our code called calculator here is also the name of our file that's on purpose code from a class goes into a file by the same name so keeps it pretty easy pretty straightforward pretty simple uh when we start to look at things okay I'm getting told by ree maybe if we can increase the the uh font just a little bit here oops let's see if I can do that um let see here well I thought I could there we go much better Okay so we've got our class what's the first first thing we're going to then do to this particular class well we're going to start to add some code inside of the code I'm going to have everybody uh type public static void main string args and more squiggly brackets okay what do we just do here we created what's called a method inside of calculator so I said the class is where all code goes inside a class we could have various let's call them operations or tasks or the formal word in Java is methods for smaller bits of code you could have lots of little chunks of code the very first one where the application kicks off is a method called main that is these the place where all Java applications like this are going to start so main is what I like to call the kickoff point for our application now Java is case sensitive so main uppercase if I can get my keyboard going here that's a whole entirely different method as far as Java is concerned could be a little confusing and a little rough on beginning Java developers but main lowercase is a point at which when the application starts Java is going to look for this method called Main and start executing code from this point from this method if we had methods they could go right inside this class as well a little bit later on we'll see if we can't add another method for right now we're going to keep everything inside of this one method inside of our class calculator to keep things simple public static void again these are terms which you're going to learn a little bit later in your Java Journey just put them there as my mom used to say do so because I said so right now and we'll learn a little bit later in our Java Journey about why those particular words are there in front of Maine again very much like public in front of the class calculator it's a way to tell the Java world that what is going to be used by others and what might be private to others and static kind of goes to um some operations that really require us to know a little bit more about how Java does objectoriented programming but we'll leave that for a later discussion we'll also leave for a later discussion this string ARG square bracket that's um what we call an argument to the method we'll talk a bit about that a little bit later on but specifically about why you need string args to a main leave that as part of a discussion uh for a later session as well just do that again because that's the way it has to be done to get started with Java so we have a class name your class whatever you'd like but you have to have a main method with public static void and string args as its argument to get started now if you'd like we can actually run this application right now this is a full-fledged Java application so if I push the Run button we'll see um the intellig IDE here try to build the application and run it and it actually pops up a terminal it says okay I ran everything it finished and it has ex Code Zero exit Code Zero is Java's way to saying everything was a okay it ran well now if we're looking at this we're probably not very excited because it didn't really do anything and of course it didn't because we really didn't do any in anything inside of Maine to accomplish any task right now but at least we know our environment is up and running and things are ready to go for us to start to do some more work okay what else do we want to take a look at here uh these curly brackets what are those all about these curly brackets Define whatever piece of code we happen to be working on it defines the beginning and ending of that piece of code so the main method has curly brackets the class has curly brackets I said that all code goes into a class and then we have lots of methods in a class well that's true but also all code has to go inside of curly brackets associated with the class and with the methods so curly brackets are our way to tell Java hey here's the beginning of in this case the beginning of Main and here's the end of main here's the end of the class and the beginning of the class so those curly brackets are very important to designate to Java the pieces of our application where they begin and end let's do something now a little bit more exciting let's actually give our application something to do so I'm going to do system.out.print line and I'm going to say hello world kind of the place where all applications for any language start is to be able to print out hello world now what does this mean system represents the computer that you're on Java has a keyword to essentially say hey computer that I'm working on give me in this case with dotout give me the output and I want to print or in this case print line that's what Ln stands for hello world to it so this is a way now to start to interact with our user so if I actually run this piece of code now now things are going to get a little bit more exciting here is our intell J IDE runs and we notice hello world comes out in what we call the terminal or the output to the user for our program couple other things we want to talk about here notice I have to supply some text or if you will what Java calls a string to that output I have to tell it what we want to print and I provide the semicolon to say that's the end of this particular command uh this is the end of this particular line of code Java designates lines of code with semicolon so it designates things like methods with curly brackets it designates the end of code with a semicolon probably the most often forgotten piece of to a new Java developer is to always forget that semicolon by the way if you didn't have that what would happen if we tried to run this application without that piece of syntax you'll notice that the IDE will come back and Java will come back tell us hey I think you forgot something this case you forgot the semicolon so never fear um your IDE and your tools in your Java world will tell you and remind you of things like that if you do forget them okay so now we've done something to uh print I might mention here too a good place for me to say well if we're printing line why are we printing line Jim well there's also I could break this up I could say uh system.out.print let's just say hello here and then say a print line world what's the difference notice even I forgot the semicolon print says go ahead and get this ready print line ends the Asal display and pushes everything out to the terminal so it's a way to start to break up if you want to print little pieces and then ultimately print the line you provide a print Line Print allows you to dump out little pieces the text starts to be formed if you will and gets displayed this case the hello world hello part and then the print line finishes it off and actually completes the whole line of text in the terminal it it's what provides what we call the U the carriage return if you're familiar with that kind of syntax the carriage return inside the terminal so either way print print line I almost always just do print line just makes things cleaner and easier when I do my work okay so we've gone through a couple of steps and we've already started to outline some pretty important syntax here with Java all code goes into class need a main method as your kickoff point system.out.print line is our way to start to interact with the user and push display out into the terminal window let's go ahead and do a little bit more here we're actually going to start to build our calculator now so let's go ahead here and change this to welcome to data camps calculator okay make it a little bit more exciting now the next thing we're going to want to do is we're going to want to start to take in data from the user if we're a calculator just like if I went to my my phone and wanted to do some calculations with the calculator app I have to get some numbers that I give it to actually provide for some sort of result I have to tell it hey take four plus five and give me a result those are called operands so what I'm going to do here is I'm going to provide a comment say we need some holders for operands now notice how the the IDE are Chang this color slash slash or if you will the comment indicator for Java tells Java hey ignore whatever you see behind the slash slash all the way to the end of this line what this is a means for we as developers to start to put little information pieces into our code into our programs to give us some information about what it is we're doing Java is going to ignore comments but comments are a good way to leave behind notes for yourself and your fellow developers about what's happening inside the application so in this case I'm leaving a note for myself that we're going to provide some holders for operands what are operands well if I wanted to add say four and five right and that's going to give me some sort of result or some four and five here are the operands all right so I need holders for those two pieces of information it means to give Java those two pieces of data and there's another piece of data and that is what's called the um the actual operation that we're going to perform this case plus so I'm going to need a means to actually hold that the way that Java does that is by declaring what's called a variable we're going to do a couple of variables here double um one I'm just going to put down zeros there what I've done here is decided that I want to call this first holder num one and I've told Java what type of information I'm going to put in that holder in this case it's going to be a double or if you will a floating Point number and I've even with the equals I've even assigned it an initial value of 0.0 so what the heck does this mean let me draw a little bit picture here if we've got a system if we've got a a program out there and it's running in memory this little uh black box is Java's memory that's going to be running our application when I Define a variable what I'm doing is I'm defining a bucket a bucket that I could put data in and I actually name that bucket as well in this case I'm naming that bucket in this case num one and when I say I can assign data to it it means that what I'm doing is allowing myself and Java to actually put data that it holds on to for me in that bucket which I can go back and ask for by name later and actually use so in this case when I say num one equals 0.0 what I'm doing is already assigning it some sort of value some data that it's going to hold on to for me I'm going to actually create another bucket here in just a second our second operated and I'm going to call that num two and I'm going to allow that also to have a value of 0.0 all right so this is our way to hold data in Java that we can use a little bit later on down the line inside of our application in in this particular case in our calculator so I'm going to have a second double called num two equals 0.0 as well okay so I've got two Opera this is my way to hold like four and five that I want to add together but I'm going to need a third uh variable this one I'm going to make to be of string type and we'll talk about these types in just a second and I'll call this one an operation and I'm GNA have that be plus by default all right just by default just for right now all right so I've created a third bucket called operation in this case that not going to be double it's going to be a string and that's what holds the thing that the operation that we want to actually perform now you might be looking at these things called Doubles and strings what's that all about when we create these buckets in Java we also have to type them that's the way we tell Java what kind of information is going to go inside of those buckets Java has lots of different types and I'm just going to bring up a diagram here of some of those types that you are going to come in contact with early on in your devel M and that is we saw double this is for fractional numbers so something like 4.5 or 6.3 whatever the case might be but you could also have just a plain old int or what we call an integer that could be a number from about two billion in the minus to positive side you can have a long which is a huge number what we're doing is telling Java how much memory to set aside for these buckets in memory so that it knows what type of information we're going to be using and what kind of uh of memory it should reserve for that information holder you might notice too there's uh booleans and chars and of course in the application we also have a string type that allows us to hold strings of information so in fact while our operation is going to be called Plus or will be as we work forward I could put my name in there for example it's just any collection of characters is a string okay so now I've got my um variables defined two doubles for the operations and one for the operation just for grins let's go ahead and system out these just so we can give ourselves some assurances that Java is actually holding on to these particular uh variable datas right now forth so let me print those all out we do num one num two and we'll do operation okay so if I actually run this now ah you can see okay welcome to data cams calculator and we see Z 0 and plus as kind of our default values just to get a started so we're well on our way to creating our calculator here all right so we've actually completed a couple of our steps here um step one and step two to get the variable set up to get our program structure uh going what's next what's what next is we have to start to get input from the user we're not going to just add zero and zero all the time we actually have to have numbers coming from our user how do we do that well I'm going to put up a couple of lines of code here and then we'll talk about it so the very first thing I want to do is I want to create um a clue to our users that they need to actually go ahead and give us a number so I'll say something like uh please enter first if I can spell uh number so my call there then I'm going to develop something called a scanner which I'm going to call input we'll talk about this here in just a second new scanner system.in what is this all about again we'll talk about what this does in just a second and then with that scanner what I'm actually going to do is say num one equals input dot next double okay and this scanner is going to want me to add something in the topic class which we'll talk about here in just a second it's going to ask me to import Java util scanner okay let's talk a little bit about what all this what this is doing right now so we've already talked about system out we know this is our prompt to the user that we wanted to enter in the first uh number scanner is Java's object to as the name implies scan something in this case it's going to scan system remember is the is the name for the object that represents our computer in opposite of out is the stream for getting information in from the user out is our way to display information out to the system or out to the user in is a way we accept data from the user and we're asking to set up a new scanner this is a Java provided um mechanism to read data from that system in in other words from the input stream we use that object or that thing if you will to then ask it to give us the next Double in other words we're assuming the user is going to give us a double A number that we can use to actually use as our operant so if I actually run this code now what happens we see in the uh output here again welcome to dat cams calculator we see the the system outs of the current uh values but then it says hey please enter your first number and it actually allows me to enter a number here so it's now taken my number and with taking that number we stuffed it back into our variable called Thum one great so we now have that first number let's represent the or get the second number here too let me do a little bit of a shortcut here let's go and ask for the second number and of course we're going to need to have one of these we're we're going to ask num two to be the second number so let's run this so please enter the first number let's go ahead and say five we'll ask for the second number we'll ask four okay and we're well on our way we've got two numbers now what's left well as you probably can imagine we ask to Define variables for two numbers and then an operation so the next thing we have to do is actually get the uh string for our operation and you guess it it's still going to come from system.out so let's uh add another prompt here for the user so please enter the operation uh if I could spell here operation there we go and now let's ask it in our operation variable to be set to now what we're getting from the input this time is not a double but a string so the means to do that is ADD ask for the next set of characters essentially which will be put into operation if we do this or run this we'll see now that we're g to get all the data we need so we're going to get four let's go ahead and ask for six is the next number and it asks for the operation let's say plus okay so now we've got the three pieces of data from our user the two operations and the operation what's left to do we have to actually perform that operation between those two operation oper brands in our code so we've completed another big step here and that is input from the users of the data now for Simplicity sake so we don't see so many things in the uh in the output I'm going to delete these uh these earlier system.out so that we keep it to kind of the interaction between we and our user in this case and that is to get the the operand and the operation what we now have to do is do something with this information remember there could be lots of operations we could add we could subtract we could multiply we could divide so we have to determine first of all what is our operation Java's control flow mechanisms allow us to determine what thing is being sent to us and from that determine what kind of other code we need to execute so I'm going to create something called a control flow if statement so if operation equals let's do uh addition first if it equals if that operation equals a plus ah so we know that if there's a a situation where what the user entered is a plus through this if statement and notice we've got new curly brackets here that's saying run the code inside these curly brackets let's do a um let's call it um double x equals num one plus num two and we'd probably like to show that result to the user so we we can do a system.out.print line again to show the user that particular value so in this case X so if the user entered in our two doubles and we've got our operation if that operation is a plus operation in other words the operation equals plus as a string then go ahead and add the two operand together to give us a result which is another double and we're going to print that out to the user so we'll go ahead and run and see part of our calculator is now starting to take shape so please enter our first number we'll enter four the second number will enter seven and our operation is plus ah and there's our answer we've got a calculator now working at least a calculator for addition all well and good how do we take care of the rest of the operations well as you can imagine what we could start to do now is actually copy this code and do an operation for minus whoops wrong place there do an operation for minus uh do something like uh number one minus that right we could do an operation for each of the various pieces we have oops sorry folks so I could do one for minus I could do one for plus I could do one for multiplication and I could do one for division I'll straighten up all this code here there we go so if it's an operation of minus then go ahead and subtract if it's an operation of plus go ahead and sum if it's a multiplication go ahead and provide the product and if it's a operation to divide go ahead and show the quotient now we've covered all of our bases with regard to our calculator for these simple operations depending on which particular operation is hit based on the equals comparison it'll perform the right operation so let's go ahead and test our calculator out so again we'll enter uh our our information here our first operan we'll am our second operan and now we'll let's pick out divide for example and we see that our result is two now in looking at this I'll make this a little bit bigger so we can see it all and we look at this one of the things we see in this is that no matter what happens Java is going to then compare the operation to each of these each and every time it's going to say hey is the operation equals and I'm going to run this code in between these curly brackets hey is the operation plus then I'll run this code that's a lot of checking that Java is doing we can actually simplify that a bit we can perform an else if what else does it says hey if I if I did not run what that first if uh conditional checked for then go try the others so let me format this now so if the operation is equal to a minus then we'll go ahead and run this code if the operation is equal to minus it will ignore the rest of these else conditions so it can make our application a little bit quicker with this is if else if type of mechanism runs the same way but it's another what we call control flow mechanism in Java to help shorten the amount of code that Java actually executes even better and to kind of bring us one step forward from from what we have uh been talking about is Java provides a means called a switch that does all this in one fell soup so I'm going to comment that out remember comments are ignored in Java and even easier way to do this is using something a control mechanism called a switch in Java switch on operation I'm going to ask Java to say hey in when you're checking this particular um operation if it happens to be a case of a plus for example then go ahead and perform I'm going to cheat here a little bit then perform this code I guess if we're doing plus we should put a number there okay what did I just do I'm going to cheat here a little bit and rather than having all these if then El's I'm going to do a switch and say if the switch based on operation happens to be case plus then perform this code I can add other cases to the switch statement let's do a minus again and a division and again a multiplication and each one of these obviously we need to actually perform the actual operation so what I've done is simplified the if then else into a switch statement another control mechanism in Java says based on the operation if it happens to be a case of the operation being a plus then run these lines of code if it happens to be a case of the operation being and a minus run these lines of code so it kind of makes things a little bit neater and Tighter and it doesn't have to go through all the checking it just goes to the particular case that matches the operation the one thing I'll have to add to this as well is something called a break what break says is hey once you've actually executed one of these cases break out of this switch and don't go trying to execute the rest of them there we go so now let's go ahead and run this uh bit of application to see if our calculator is fully Done Yet wh what did I do oh I forgot I should probably have set up double x equals 0.0 to start then we don't need all these declarations in there there we go let's try that now okay so enter our first number five enter our second number two and we'll do a multiplication this time and there we go our result is 10 so now we have a fully-fledged Java calculator that is um working for us let's see we got a couple minutes left uh in that time what I want to cover uh one last thing here and that is well our calculator is pretty good but not perfect have we thought about the case where maybe the user Goofs a little bit that is let's say that the user decides hm my first number I want to be the letter Q oh bad news right it's not a double and Java doesn't know what to do with that and so what happens is Java creates an error or what we call an exception in Java in this case it says I don't know what to do with Q as your first number or more formally it says there's an input mismatch here I do not know what to do with this double called Q how do we handle that in Java Java actually has a means to handle these kinds of problems it's called exception handling now exception handling is a little bit Advanced here so I'm going to kind of cover this in a little bit of a quick fashion but I'll encourage you to to study this as you start to get deeper into Java but what we can do when we start to look at any particular piece of code is we can ask Java to try to run a piece of code and this case try to get an input for a particular double number and if that doesn't work catch uh the problem or the error or what we call exception in Java and do some sort of action and am I still online Reese can you hear me I see a oops connection lost hello hello Rich you there reach from Ry I I can't hear anybody sorry Jim no we're we're okay okay very good my signal was lost so okay um so just to wrap up here gang we're gonna we're going to when we're trying to do something like get the uh the next Double since the user might enter bad data we're going to try to get that input but then we're going to ask Java to catch the exception if something goes wrong and in this case when we catch the exception we're going to say something like and we'll just make something up here something like did you enter a wrong number now with this try catch mechan what we're actually uh doing is providing Java the means to try to execute a piece of code but if it does encounter some sort of problem it's going to catch that exception actually I've got my brackets here in the wrong place there we go whoops if I spell it right catch the problem or catch the error or catch the exception and perform some other code so now let's go ahead and give this uh particular code a run with this check in place we're only doing it on the first number but now if I try to enter the first number Q it's going to say whoops hey you got a problem there if we take a look at the output did you enter a wrong number then of course it tries to enter the second number and it still got other problems what we probably got to do is say hey you entered the wrong number and then use our system mechanism to say let's leave the application at this point we can't really do anything more so let's exit the program but at least let the user know that they've probably entered the wrong data so now we run our application and we're giving Java with this try catch mechanism a means to recover at least gracefully from problems that are created inside our application a little bit more of an advanced topic but it means for you to start to deal with issues in Java which is one of its strengths it not only allows you to code and write applications perform actions but to also try to catch and deal with problems as they occur okay we have gone through a lot of information here today um let me switch back here really quickly before we start to uh take some questions uh so get your questions ready but we've looked at the programming structure for Java we've looked at how to comment we've looked at variables and assignments and how Primitives and string work we've also looked at simple terminal input and output we've looked at some control flow which is if then and switch and a little bit of exception handling whoops um want to learn more about Java couple of places on the internet out there to go uh bing.com is a great website dedicated to Java all sorts of topics from beginners to Advanced and assistance from Java developers like me out there who will help you with the questions you might have Oracle are the stewards of java you can find their particular website is very useful in looking up and finding about information about Java and where to turn for more dev. Java is a developers news and event site and then openjdk is the community dedicated free an open source implementation Java and they've got lots of information on Java out there as well if you're inclined to to read a book or maybe get a book and read it on your um your iPad or what have you um there are a couple books I recommend to beginners head first Java uh learn Java in one day and Java for beginners are all excellent resources for picking up that first set of java lessons that get you on your way and of course I hope you will join us at D camp where we've now got two and a soon to coming uh third class on Java so we've got introduction to Java introduction to objectoriented programming in Java and there's a class we're working on right now called data types exceptions in Java that will be coming soon so plenty of pathway in data Camp to learn more about Java I hope today's session's been uh good for you hope you've had a chance to learn a bit about programming and about how Java does programming my emails here if you've got questions please feel free to forward me anything you'd like to chat about and with that I'll open it up and Richie I not sure if we have any um additional questions coming in right now I didn't see any but U I might have missed them Richie I think you're still on mute at least I'm not hearing you all right I'm I'm ignoring the I'm that works issues so uh yeah back up mic for now uh yeah for anyone in the audience please do uh add some questions to the chat uh we've got a a few minutes left for those in the meantime uh that was brilliant jim I've got a few questions for you um so my first question is around the workflow so I saw that um you were kind of typing a bit of code and then you were clicking like build and run um so and there was a kind of frequent Loop there like what is that the standard flow like how much code should you be writing before you go and test it yeah great question uh Richie and the answer is um I tend to do that in very small chunks there are other people I know so my cohorts are much better at programming than me who might type out a big chunk and then try to run it and then they get frustrated when there's all sorts of errors and things of that nature so I always encourage especially students of mine keep it small right keep little chunks in the way that these idees and Java works you can add a little test a little add a little test a little all day long and before you know you've got a big application that's not too hard to put together as opposed to typing in a lot and then realizing oh my goodness I've got all sorts of problems but really there is no wrong or right way of doing things it's just kind of uh something you kind of develop based on your own preferences as you go along um okay wonderful and uh do you have any advice on what to do when your code goes wrong yeah when code goes wrong um the wonderful thing about tools like intj or almost any IDE you get uh in the Java world is it has all sorts of debugging capability all sorts of tools to try and figure out those problems and as you notice even when you know I did things like leave off a semicolon in in my code it quickly let me know hey you got a problem here right it's got the little red and it's got a red light bulb all the IDS do that kind of thing take some practice to get used to looking for those kinds of things but pretty quickly you learn to uh deal with those and if you don't when you go to run and build as we saw during one of my examples it'll let you know pretty quickly um hey dummy you forgot something there so it's again just kind of a matter of practice but all the IDS really help you out in that way uh that that is good to know that uh you do get some help from the idees uh couple of questions from the audience so uh let's take this one first from Bina so um B is asking about uh can you use job in data lab short answer is no I mean there might be ways around if you're very clever but uh dat lab not ready for for Java so um yeah uh uh Jim can you just talk us through uh why we're using jet uh Jet Jet brains and other any other Ides that you like for Java yeah yeah great question there are plenty of Ides out there um the ones that I like tend to be uh open source uh free to use with lots of community help so jet brains intell is one that we're using here the other one is from the eclipse foundation so it's called The Eclipse IDE those are the two that I use most often but by the way folks you'll find that even things like uh standard editors today if you use something like textpad or notepad believe or not there's plugins behind the scenes you can add to those simple editors to have them do Java coding as well go out to your favorite browser choice on the internet Java idees may be put in the word free or open source behind that if you don't want to spend a lot of money and you'll you'll get the ones that are pretty popular if you do have some some cash and you don't mind uh departing with it to buy your IDE you'll find even more out there in the Open Marketplace Oracle offers one jet brains offers one obviously the ones you pay for will some time to offer a few more bells and whistles than the open source ones but most of the time they all have to do the same thing that is allow you to enter code compile and run it and keep doing that until you get your application done yeah I feel like the uh the paid ones tend to offer enterprise features like collaboration and things like that so if you just aren't like coding by yourself then I think the pre- ones are absolutely fine that's a good observation yeah Richie yeah you you will find most time those big Enterprise versions are doing Enterprise development for teams uh where you need to share and and cross communicate on coding if you're just doing it for yourself most the time open source will do you just fine absolutely all right so there is a question here from Dollar Cash Club good name uh I would mind some uh some dollars uh so are there any Java tracks on on data Camp so not yet there are a few courses uh starting with Jim's introduction to Java course there's Java tracks on the way uh but the the first track isn't yet complete uh so yeah watch this space um okay and let me see there's one more question from Bina uh so are the colors in coding def finded anywhere I wonder why certain colors and uh uh like words and spaces are different colors great question yeah great great question great observation too by the way all the IDS are a little bit different and there's actually a way to go into your IDs and the settings and um provide for colors that you like maybe you know you want you want comments to be in in one color the default you know has comments in blue it has um keywords like double and new and try in Orange that's that's intell's way of doing things but yes most idees will color code Things based on what it is that they represent uh things like code that you enter you can see that's kind of uh White in other words that you define that you want something to done that's white uh what we call keywords or Reserve words that are part of the language are in orang orange the colors don't matter so much as just as a means to distinguish it from other pieces of your application once you start to become familiar with Java and once you start become familiar with things like the keywords and your coding style you can go in and change those colors in your ID to make them more meaningful to you you know maybe you want to maybe you want numbers to stand out to you so you make sure you got all your data right and you can make those red for example it really is up to you uh absolutely so uh yeah I do like those sort of customizability features just uh make things uh look just how you want them to uh you can spend hours tweaking these sort of things uh anyway uh we are at time now before everyone dashes off though I want to say we've got some more webinars coming up so tomorrow we've got a more of a manager Focus session on uh assessing your organizations's AI maturity to see how good is your organization at making use of AI uh and then next week we've got um a codog session on creating an AI agent for financial reporting uh that's on Tuesday and then next Wednesday we're doing an AI Showdown so it's going to be uh making use of chat gbt the 03 Mini model and then the Deep seek R1 model so we see which one of those two is best at reasoning about data use cases uh and then on Friday next week we've got a session on building dashboards with powerbi and da so lots to look forward to there I hope you all come back uh in the meantime uh that was brilliant I hope you enjoyed getting started with Java uh thank you so much JY very very cool stuff sorry about the technical problems with the microphone I going to start bashing things with a hammer and seeing if that fixes anything uh hope it's fixed for next time uh see you all again soon all right bye
Original Description
Code Along with Us! Notebook + resources: http://bit.ly/DatacampJavaIntro
Java is a powerful and flexible programming language that is widely used to build reliable, scalable applications. In this session, you'll dive into creating a simple calculator application, offering an excellent opportunity to learn Java’s essential concepts. Topics covered include Java primitives, Strings, arithmetic operations, and control flow. This hands-on experience is ideal for aspiring software developers and data engineers looking to begin their Java educational journey.
Led by Jim White, a seasoned software developer, this code-along session guides you step-by-step in building a calculator in Java. Along the way, you'll learn core programming concepts and pickup on some useful debugging tips. By the end of the session, you’ll have not only built your first Java application but also acquired the skills to confidently explore more complex projects.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Why I Ditched Socket.IO for Raw WebSockets (And What I Learned)
Dev.to · Nikhil Sharma
atob() can't decode a JWT — the Base64URL gotcha (and the fix)
Dev.to · Daniel Cheong
Why Debugging Made Me a Better Developer
Medium · JavaScript
Mapping Go Domain Errors to HTTP Status Codes at the Boundary
Dev.to · Gabriel Anhaia
🎓
Tutor Explanation
DeepCamp AI