Learning a New Language – Java!

Last week, I wrote about my job hunt process and what I have been doing during so. In this industry where one can never learn enough and know everything, it is important to familiarize yourself with the different patterns and styles of the many languages that exist. This has taught me how similar patterns can be and it all lies in the syntax.

This week I will walk through some of the parts I found most interesting while learning the basics of Java. A factor I always look for is the data types and their size to make sure my application is as efficient as it can be. For Java, I look to this Primitive Types diagram to use as a guide:

Another difference I have noticed across languages is in the CLI. In Java, we use the Scanner class which is used to get user input, and it is found in the java.util package.

Example:
To ask the users their age and return “You are __” with their input age.

<span>Scanner</span> <span>scanner</span> <span>=</span> <span>new</span> <span>Scanner</span><span>(</span><span>System</span><span>.</span><span>in</span><span>);</span>
<span>System</span><span>.</span><span>out</span><span>.</span><span>print</span><span>(</span><span>"</span><span>Enter your age: </span><span>"</span><span>);</span>
<span>byte</span> <span>yourAge</span> <span>=</span> <span>scanner</span><span>.</span><span>nextByte</span><span>();</span>
<span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>"</span><span>You are </span><span>"</span> <span>+</span> <span>yourAge</span><span>);</span>
 <span>Scanner</span> <span>scanner</span> <span>=</span> <span>new</span> <span>Scanner</span><span>(</span><span>System</span><span>.</span><span>in</span><span>);</span>
        <span>System</span><span>.</span><span>out</span><span>.</span><span>print</span><span>(</span><span>"</span><span>Enter your age: </span><span>"</span><span>);</span>
        <span>byte</span> <span>yourAge</span> <span>=</span> <span>scanner</span><span>.</span><span>nextByte</span><span>();</span>
        <span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>"</span><span>You are </span><span>"</span> <span>+</span> <span>yourAge</span><span>);</span>
Scanner scanner = new Scanner(System.in); System.out.print("Enter your age: "); byte yourAge = scanner.nextByte(); System.out.println("You are " + yourAge);

Enter fullscreen mode Exit fullscreen mode

In general, these are notes that anyone starting to learn Java can use or come back to for a refresher on data types and user inputs.

Here is a link to the Java Tutorial for Beginners I used to get started!

原文链接:Learning a New Language – Java!

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
Only his strong enough, will not be trampled.
只有自己足够强大,才不会被别人践踏
评论 抢沙发

请登录后发表评论

    暂无评论内容