<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1082035720476633097</id><updated>2026-04-07T18:19:43.454+05:30</updated><category term="Java"/><category term="Spring Boot"/><category term="Spring Batch"/><category term="hibernate"/><category term="Data Structure"/><category term="Design Patterns"/><category term="JPA"/><category term="MongoDB"/><category term="Git"/><category term="Spring Data"/><category term="array"/><category term="string"/><title type='text'>Walking Techie</title><subtitle type='html'>Blog about Java programming, Design Pattern, and Data Structure.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>162</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-995299048473491356</id><published>2020-08-23T19:26:00.001+05:30</published><updated>2020-08-23T19:29:04.955+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Git"/><title type='text'>git-log useful options</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
    &lt;h1 style=&quot;color:blue&quot;&gt;What does git log do?&lt;/h1&gt;
    &lt;p&gt;The git log command display all the commits history in a repository.&lt;/p&gt;
    &lt;p&gt;By default, the command displays for each commits:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;Secure Hash Algorithm (SHA)&lt;/li&gt;
        &lt;li&gt;author&lt;/li&gt;
        &lt;li&gt;date&lt;/li&gt;
        &lt;li&gt;commit message&lt;/li&gt;
    &lt;/ul&gt;
    &lt;h1 style=&quot;color:blue&quot;&gt;Git log options&lt;/h1&gt;
    &lt;p&gt;You can customize the information provided by &lt;code&gt;git-log&lt;/code&gt; by passing the options.&lt;/p&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;--oneline&lt;/h2&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log --oneline
    &lt;/div&gt;
    &lt;p&gt;The &lt;code&gt;--oneline&lt;/code&gt; option causes &lt;code&gt;git log&lt;/code&gt; to display each commit in one line&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;The first seven characters of SHA&lt;/li&gt;
        &lt;li&gt;The commit message&lt;/li&gt;
    &lt;/ul&gt;
    &lt;pre&gt;8fc4335 first commit
09cca7e Initial commit
  &lt;/pre&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;--patch or -p&lt;/h2&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log --patch
    &lt;/div&gt;
    &lt;p&gt;or, the shorter version&lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log -p
    &lt;/div&gt;
    &lt;p&gt;The &lt;code&gt;--patch&lt;/code&gt; option causes &lt;code&gt;git log&lt;/code&gt; to display&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;Commit id, Author, Date, and commit message.&lt;/li&gt;
        &lt;li&gt;The files have been modified, addition/deletion in files.&lt;/li&gt;
        &lt;li&gt;The specific changes that happened in this commit. Like addition and deletion of changes.&lt;/li&gt;
        &lt;li&gt;Line number where addition/deletion happened.&lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;You can see something like below, when you run &lt;code&gt;git log --patch&lt;/code&gt;&lt;/p&gt;
    &lt;pre&gt;Author: Santosh &lt;santosh@gmail.com&gt;
Date:   Sun Aug 23 18:02:21 2020 +0530

    initial commit

diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
  &lt;/pre&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;-- &amp;lt;absolute/relative path of file&amp;gt;&lt;/h2&gt;
    &lt;p&gt;When you want to see the changes of specific file then you can write git log -- file name. Make sure that there
        is space between the -- and file name.&lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log -- gradle.properties
    &lt;/div&gt;
    &lt;p&gt;The &lt;code&gt;-- filename&lt;/code&gt; causes &lt;code&gt;git log&lt;/code&gt; to display only specific commit happened to this file
        only&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;Secure Hash Algorithm&lt;/li&gt;
        &lt;li&gt;author&lt;/li&gt;
        &lt;li&gt;date&lt;/li&gt;
        &lt;li&gt;commit message&lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;This will display recent commit first or can say in descending order of timestamp.&lt;/p&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;--oneline &amp;lt;absolute/relative path of file&amp;gt;&lt;/h2&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log --oneline gradle.properties
    &lt;/div&gt;
    &lt;p&gt;The &lt;code&gt;--oneline filename&lt;/code&gt; causes &lt;code&gt;git log&lt;/code&gt; to display only specific commit happened to this
        file
        only&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;The first seven characters of SHA&lt;/li&gt;
        &lt;li&gt;The commit message&lt;/li&gt;
    &lt;/ul&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;Commit made by particular author&lt;/h2&gt;
    &lt;p&gt;You can see the commit made by specific user using the below command.&lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log --author&quot;author-name&quot;
    &lt;/div&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;Start at specific commit&lt;/h2&gt;
    &lt;p&gt;You can display display the commit with specific SHA and all of the commits made before that commit.&lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log SHA
    &lt;/div&gt;
    &lt;p&gt;You can combine with other available options.&lt;/p&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;Search commit message&lt;/h2&gt;
    &lt;p&gt;You can search commit message using the &lt;code&gt;--grep&lt;/code&gt;.&lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log --grep=&quot;first commit&quot;
    &lt;/div&gt;
    &lt;h2 style=&quot;color:black&quot;&gt;--graph&lt;/h2&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log --graph
    &lt;/div&gt;
    &lt;p&gt;The &lt;code&gt;--graph&lt;/code&gt; options enables you to view your &lt;code&gt;git log&lt;/code&gt; as a graph. You can combine this
        with &lt;code&gt;--oneline&lt;/code&gt; option you have learned from above. &lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;
        &amp;nbsp; &amp;nbsp;$ git log --graph --online
    &lt;/div&gt;
    &lt;p&gt;The &lt;code&gt;--graph&lt;/code&gt; option is very useful in &lt;code&gt;git log&lt;/code&gt; when you are working on feature on which
        multiple developers are working.&lt;/p&gt;
    &lt;p&gt;You can combine with &lt;code&gt;--decorate&lt;/code&gt; option.&lt;/p&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/995299048473491356/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/08/git-log-useful-options.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/995299048473491356'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/995299048473491356'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/08/git-log-useful-options.html' title='git-log useful options'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-5122063337642401687</id><published>2020-07-25T16:19:00.001+05:30</published><updated>2022-05-10T23:09:44.850+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Git"/><title type='text'>How to see git configuration</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;p&gt;&lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/07/how-to-install-and-configure-git-on-ubuntu.html&quot;
      target=&quot;_blank&quot;&gt;In the last post&lt;/a&gt;, we have seen how to install &lt;code&gt;Git&lt;/code&gt; into Ubuntu
    machine and some configuration of it. In this post we will see more about config.&lt;/p&gt;
  &lt;h1 style=&quot;color:blue&quot;&gt;How to get username and email&lt;/h1&gt;
  &lt;p&gt;We have set the user name and email in the last post. How can we get the username
    and email to verify it?&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --get user.name
  &lt;/div&gt;
  &lt;p&gt;This will return the username. Username can be picked up from local config if you are in
    repository otherwise it will pick from global configuration.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --get user.email
  &lt;/div&gt;
  &lt;p&gt;This will return the email of user. Email can be picked up from local config if you are in
    repository otherwise it will pick from global configuration.&lt;/p&gt;
  &lt;p&gt;You can fetch user name and email from specific configuration by passing the options. You can
    do like below.&lt;/p&gt;
  &lt;p&gt;To get from global configuration file. Need to pass &lt;code&gt;--global&lt;/code&gt;.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --global --get user.name
  &lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --global --get user.email
  &lt;/div&gt;
  &lt;p&gt;To get from local configuration file. Need to pass &lt;code&gt;--local&lt;/code&gt;. To run the below command, you must
    be in local repository.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --local --get user.name
  &lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --local --get user.email
  &lt;/div&gt;
  &lt;h1 style=&quot;color:blue&quot;&gt;List Git Settings&lt;/h1&gt;
  &lt;p&gt;You can print the Git setting using the below command.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --list
  &lt;/div&gt;
  &lt;p&gt;You are in local repository and run this command. It will list all the local, global and system
    config. But if you are not in git directory then it will list system and global config of
    git.&lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;List Git Settings by config&lt;/h2&gt;
  &lt;p&gt;To print the system config. pass the &lt;code&gt;--system&lt;/code&gt; option.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --system --list
  &lt;/div&gt;
  &lt;p&gt;To print the global user config. pass the &lt;code&gt;--global&lt;/code&gt; option.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --global --list
  &lt;/div&gt;
  &lt;p&gt;To print the local user config. pass the &lt;code&gt;--local&lt;/code&gt; option. You must be in git repo.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --local --list
  &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/5122063337642401687/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/07/how-to-see-git-configuration.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/5122063337642401687'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/5122063337642401687'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/07/how-to-see-git-configuration.html' title='How to see git configuration'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-2616955786766414410</id><published>2020-07-15T21:27:00.005+05:30</published><updated>2022-05-10T22:59:26.797+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Git"/><title type='text'>How To Install and Configure Git on Ubuntu 18.04</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;What is Git?&lt;/h2&gt;
  &lt;p&gt;Git is currently the most popular implementation of a distributed version control system.&lt;/p&gt;
  &lt;p&gt;Git originates from the Linux kernel development and was founded in 2005 by Linus Torvalds.&lt;/p&gt;
  &lt;p&gt;The core of Git was originally written in the programming language C, but Git has also been
    re-implemented in other languages, e.g., Java, Ruby and Python.&lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;distributed version control system?&lt;/h2&gt;
  &lt;p&gt;In a distributed version control system each user has a complete local copy of a repository on
    his individual computer. The user can copy an existing repository. This copying process is
    typically called cloning.&lt;/p&gt;
  &lt;p&gt;Every clone contains the full history of the collection of files and a cloned repository has
    the same functionality as the original repository.&lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Install git in local machine&lt;/h2&gt;
  &lt;p&gt;You can install git in any OS based machine. I am going to install git in Ubuntu machine.&lt;/p&gt;
  &lt;p&gt;Command to install git in Ubuntu machine.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ sudo apt-get install git
  &lt;/div&gt;
  &lt;p&gt;Hit enter. It will prompt for password. Enter it. It will take few seconds to install it.&lt;/p&gt;
  &lt;p&gt;Once Git installed in machine. It will serve as both an agent list server as well as a client.
    Nothing extra we need to install for a server.&lt;/p&gt;
  &lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
  &lt;ins class=&quot;adsbygoogle&quot;
       style=&quot;display:block; text-align:center;&quot;
       data-ad-format=&quot;fluid&quot;
       data-ad-layout=&quot;in-article&quot;
       data-ad-client=&quot;ca-pub-3454920994838217&quot;
       data-ad-slot=&quot;9892102787&quot;&gt;&lt;/ins&gt;
  &lt;script&gt;
        (adsbygoogle = window.adsbygoogle || []).push({});
    &lt;/script&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Uninstall git&lt;/h2&gt;
  &lt;p&gt;To remove just git package itself from Ubuntu 18.04 execute on terminal:&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ sudo apt-get remove git
  &lt;/div&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Uninstall git and it&#39;s dependent packages&lt;/h2&gt;
  &lt;p&gt;To remove the git package and any other dependant package which are no longer needed from
    Ubuntu.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ sudo apt-get remove --auto-remove git
  &lt;/div&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Purging git&lt;/h2&gt;
  &lt;p&gt;If you want remove git package, configuration and/or data files of git from Ubuntu.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ sudo apt-get purge git
  &lt;/div&gt;
  &lt;p&gt;To remove git package, it&#39;s dependencies, configuration and/or data files of git from
    Ubuntu.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ sudo apt-get purge --auto-remove git
  &lt;/div&gt;
  &lt;br/&gt;
  &lt;iframe width=665 height=374 src=&quot;https://www.youtube.com/embed/EuFPe24-BXo?feature=oembed&quot; frameborder=0 allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Configuration of Git&lt;/h2&gt;
  &lt;p&gt;You need to logged as root user in your system to set up some global configurations. Git
    will use some of the global parameters. Default parameters will use when we will use git
    repository, unless there are local overrides that are provided as part of local git config or
    repository.&lt;/p&gt;
  &lt;p&gt;Here we will be configuring global user name and email. So that when we are not providing the
    name and email configuration in repository git config. It will take from global configuration
    when committing, checkout and push the code to origin or upstream.&lt;/p&gt;
  &lt;h3 style=&quot;color:blue&quot;&gt;Config file and location&lt;/h3&gt;
  &lt;p&gt;Git use the config file based on level of configuration. These are the below config
    levels.&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;system&lt;/li&gt;
    &lt;li&gt;global&lt;/li&gt;
    &lt;li&gt;local&lt;/li&gt;
  &lt;/ul&gt;
  &lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
  &lt;ins class=&quot;adsbygoogle&quot;
       style=&quot;display:block; text-align:center;&quot;
       data-ad-format=&quot;fluid&quot;
       data-ad-layout=&quot;in-article&quot;
       data-ad-client=&quot;ca-pub-3454920994838217&quot;
       data-ad-slot=&quot;9892102787&quot;&gt;&lt;/ins&gt;
  &lt;script&gt;
        (adsbygoogle = window.adsbygoogle || []).push({});
    &lt;/script&gt;
  &lt;h4 style=&quot;color:blue&quot;&gt;Below are the config files, location, and its meaning.&lt;/h4&gt;
  &lt;dl&gt;
    &lt;dt&gt;$(prefix)/etc/gitconfig&lt;/dt&gt;
    &lt;dd&gt;System wide configuration file.&lt;/dd&gt;
    &lt;dt&gt;$XDG_CONFIG_HOME/git/config&lt;/dt&gt;
    &lt;dd&gt;Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty,
      $HOME/.config/git/config will be used.
      &lt;p&gt;Any single-valued variable set in this file will be overwritten by whatever is in
        ~/.gitconfig.&lt;/p&gt;
    &lt;/dd&gt;
    &lt;dt&gt;~/.gitconfig&lt;/dt&gt;
    &lt;dd&gt;User-specific configuration file. Also called &quot;global&quot; configuration file.&lt;/dd&gt;
    &lt;dt&gt;$GIT_DIR/config&lt;/dt&gt;
    &lt;dd&gt;Repository specific configuration file.&lt;/dd&gt;
  &lt;/dl&gt;
  &lt;h4 style=&quot;color:blue&quot;&gt;Repository config options:&lt;/h4&gt;
  &lt;dl&gt;
    &lt;dt&gt;--system&lt;/dt&gt;
    &lt;dd&gt;This option use the system config file.
      &lt;p&gt;For writing options: write to system-wide $(prefix)/etc/gitconfig rather than the
        repository .git/config.&lt;/p&gt;
      &lt;p&gt;For reading options: read only from system-wide $(prefix)/etc/gitconfig rather than from
        all available files.&lt;/p&gt;&lt;/dd&gt;
    &lt;dt&gt;--global&lt;/dt&gt;
    &lt;dd&gt;This option use the global configuration file.
      &lt;p&gt;For writing options: write to global ~/.gitconfig file rather than the repository
        .git/config, write to $XDG_CONFIG_HOME/git/config file if this file exists and the
        ~/.gitconfig file doesn’t.&lt;/p&gt;
      &lt;p&gt; For reading options: read only from global ~/.gitconfig and from
        $XDG_CONFIG_HOME/git/config rather than from all available files.&lt;/p&gt;
    &lt;/dd&gt;
    &lt;dt&gt;--local&lt;/dt&gt;
    &lt;dd&gt;Use repository config file.
      &lt;p&gt;For writing options: write to the repository .git/config file. This is the default
        behavior.&lt;/p&gt;
      &lt;p&gt;For reading options: read only from the repository .git/config rather than from all
        available files.&lt;/p&gt;
    &lt;/dd&gt;
  &lt;/dl&gt;
  &lt;h3 style=&quot;color:blue&quot;&gt;how to set git config username and email?&lt;/h3&gt;
  &lt;p&gt;git config - Get and set repository or global options.&lt;/p&gt;
  &lt;p&gt;Set the git global parameter for username.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --global user.name &quot;Santosh&quot;
  &lt;/div&gt;
  &lt;p&gt;Set the git global parameter for email.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --global user.email &quot;walkingtechie.blogspot@gmail.com&quot;
  &lt;/div&gt;
  &lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
  &lt;ins class=&quot;adsbygoogle&quot;
       style=&quot;display:block; text-align:center;&quot;
       data-ad-format=&quot;fluid&quot;
       data-ad-layout=&quot;in-article&quot;
       data-ad-client=&quot;ca-pub-3454920994838217&quot;
       data-ad-slot=&quot;9892102787&quot;&gt;&lt;/ins&gt;
  &lt;script&gt;
        (adsbygoogle = window.adsbygoogle || []).push({});
    &lt;/script&gt;
  &lt;p&gt;We can also set the local git configuration username and email. Make sure that you are inside a
    git repository. Otherwise it will give fetal error like; &quot;fatal: --local can only be used inside
    a git repository&quot;.&lt;/p&gt;
  &lt;p&gt;Set the git local parameter for username.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --local user.name &quot;Santosh&quot;
  &lt;/div&gt;
  &lt;p&gt;Set the git local parameter for email.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --local user.email &quot;walkingtechie.blogspot@gmail.com&quot;
  &lt;/div&gt;
  &lt;h3 style=&quot;color:blue&quot;&gt;how to set git system config?&lt;/h3&gt;
  &lt;p&gt;Let&#39;s suppose you want to set the &quot;vi&quot; or &quot;vim&quot; system editor. So whenever you edit something
    it will always open that editor by default. Here, I am setting &quot;vi&quot; editor for that check where
    &quot;vi&quot; editor is located.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ which vi
  &lt;/div&gt;
  &lt;p&gt;You will get output something like &quot;/usr/bin/vi&quot;.&lt;/p&gt;
  &lt;p&gt;Now set the default &quot;vi&quot; editor for git.&lt;/p&gt;
  &lt;div class=&quot;box&quot;&gt;
    &amp;nbsp; &amp;nbsp;$ git config --system core.editor &quot;/usr/bin/vi&quot;
  &lt;/div&gt;
  &lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
  &lt;ins class=&quot;adsbygoogle&quot;
       style=&quot;display:block; text-align:center;&quot;
       data-ad-format=&quot;fluid&quot;
       data-ad-layout=&quot;in-article&quot;
       data-ad-client=&quot;ca-pub-3454920994838217&quot;
       data-ad-slot=&quot;9892102787&quot;&gt;&lt;/ins&gt;
  &lt;script&gt;
        (adsbygoogle = window.adsbygoogle || []).push({});
    &lt;/script&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/2616955786766414410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/07/how-to-install-and-configure-git-on-ubuntu.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2616955786766414410'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2616955786766414410'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/07/how-to-install-and-configure-git-on-ubuntu.html' title='How To Install and Configure Git on Ubuntu 18.04'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/EuFPe24-BXo/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-4141583246179656092</id><published>2020-06-05T23:02:00.000+05:30</published><updated>2020-06-05T23:07:12.529+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><title type='text'>Difference between save and persist method in Hibernate</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;p&gt;Both &lt;code&gt;save&lt;/code&gt; and &lt;code&gt;persist&lt;/code&gt; methods of &lt;code&gt;Session&lt;/code&gt; class persist
    given transient instance into database.&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;The first difference between these two methods are their &lt;b&gt;return&lt;/b&gt; type. Return type of
      &lt;code&gt;save&lt;/code&gt; method is &lt;code&gt;Serializable&lt;/code&gt; object while return type of &lt;code&gt;persist&lt;/code&gt;
      method is &lt;code&gt;void&lt;/code&gt;.
    &lt;/li&gt;
    &lt;li&gt;Both method persist given transient instance into database. However, persist method doest
      guarantee identifier value will be assigned to persistence instance immediately, the
      assignment might happen at flush time.
    &lt;/li&gt;
    &lt;li&gt;&lt;code&gt;save&lt;/code&gt; operation cascades to associated instances if the association is mapped
      with &lt;code&gt;cascade=&quot;save-update&quot;&lt;/code&gt;. While &lt;code&gt;persist&lt;/code&gt; operation cascades to
      associated instances if the association is mapped with &lt;code&gt;cascade=&quot;persist&quot;&lt;/code&gt;.
    &lt;/li&gt;
    &lt;li&gt;The &lt;code&gt;save&lt;/code&gt; method persist a given transient instance inside and outside of a
      transaction boundary. While, The &lt;code&gt;persist&lt;/code&gt; method does not execute a insert query
      if it is outside of a transaction boundary.
    &lt;/li&gt;
    &lt;li&gt;The persist method is called outside of transaction boundaries, it is useful in long-running
      conversations with an extended Session context. On the other hand save method is not good in a
      long-running conversation with an extended Session context.
    &lt;/li&gt;
    &lt;li&gt;The &lt;code&gt;save&lt;/code&gt; method is only supported by Hibernate while &lt;code&gt;persist&lt;/code&gt;
      method is supported by Hibernate and JPA too.
    &lt;/li&gt;
  &lt;/ol&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Example of save in Hibernate&lt;/h2&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Person.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = &amp;quot;person&amp;quot;)
@Getter
@Setter
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;
}&lt;/pre&gt;
  &lt;pre class=&quot;brush: java; title:&#39;SaveClient.java&#39;&quot;&gt;package com.walking.techie;

import com.walking.techie.entity.Person;
import com.walking.techie.utils.HibernateUtil;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class SaveClient {

  public static void main(String[] args) {
    // get session factory of an application
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    // Open a session
    Session session = sessionFactory.openSession();

    // Begin a unit of work and return the associated Transaction object.
    Transaction transaction = session.beginTransaction();
    Person person = new Person();
    person.setName(&amp;quot;Santosh&amp;quot;);
    Serializable id = session.save(person);
    System.out.println(&amp;quot;Person saved in DB with return serialized id: &amp;quot; + id);

    // commit the transaction
    transaction.commit();
    // End the session by releasing the JDBC connection and cleaning up.
    session.close();
  }
}&lt;/pre&gt;
  &lt;p&gt;Once you run this application. You will see on console something like below:&lt;/p&gt;
  &lt;pre class=&quot;brush: java;&quot;&gt;Hibernate: create table person (id bigint not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB
Hibernate: insert into person (name) values (?)
22:45:26,649 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [Santosh]
Person saved in DB with return serialized id: 1&lt;/pre&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Example of persist in Hibernate&lt;/h2&gt;
  &lt;p&gt;We will use the same &lt;code&gt;Person&lt;/code&gt; &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/06/hibernate-entity-types-and-value-types.html&quot;
      target=&quot;_blank&quot;&gt;entity&lt;/a&gt; for this example also.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;PersistClient.java&#39;&quot;&gt;package com.walking.techie;

import com.walking.techie.entity.Person;
import com.walking.techie.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class PersistClient {

  public static void main(String[] args) {
    // get session factory of an application
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    // Open a session
    Session session = sessionFactory.openSession();

    // Begin a unit of work and return the associated Transaction object.
    Transaction transaction = session.beginTransaction();
    Person person = new Person();
    person.setName(&amp;quot;Mohan&amp;quot;);
    session.persist(person);
    // commit the transaction
    transaction.commit();
    // End the session by releasing the JDBC connection and cleaning up.
    session.close();
  }
}&lt;/pre&gt;
  &lt;pre class=&quot;brush: java;&quot;&gt;Hibernate: insert into person (name) values (?)
22:49:29,994 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [Mohan]&lt;/pre&gt;
  &lt;p&gt;After running the both examples. We have records like this in &lt;b&gt;&quot;person&quot;&lt;/b&gt; table.&lt;/p&gt;
  &lt;img border=&quot;0&quot;
       src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSCEKKvSCaMg1sAIQiVXz86_mgV4MsoOL4PY_F_ngag3b9IW6IfEJQu7_BmMx11xUUB3hg7tq0gmFwIkP8vJ6r5s4Kf4dhMUSPE3eNPAYFLf_fPIBGHSqiwOW1Qbd3dEFr7_VZfjM69tY/s1600/person_table_save_persist.png&quot;
       style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Record in person table&quot;/&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/4141583246179656092/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/difference-between-save-and-persist.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4141583246179656092'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4141583246179656092'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/difference-between-save-and-persist.html' title='Difference between save and persist method in Hibernate'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSCEKKvSCaMg1sAIQiVXz86_mgV4MsoOL4PY_F_ngag3b9IW6IfEJQu7_BmMx11xUUB3hg7tq0gmFwIkP8vJ6r5s4Kf4dhMUSPE3eNPAYFLf_fPIBGHSqiwOW1Qbd3dEFr7_VZfjM69tY/s72-c/person_table_save_persist.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-3210416001972098449</id><published>2020-06-03T23:25:00.000+05:30</published><updated>2020-06-03T23:26:55.987+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="JPA"/><title type='text'>@AttributeOverride and @AttributeOverrides example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;h1 style=&quot;color:blue&quot;&gt;@AttributeOverride annotation&lt;/h1&gt;
  &lt;p&gt;Used to override the mapping of a Basic (whether explicit or default) property or field or Id
    property or field.
  &lt;p&gt;
  &lt;p&gt;May be applied to an entity that extends a mapped superclass or to an embedded field or
    property to override a basic mapping or id mapping defined by the mapped superclass or
    embeddable class (or embeddable class of one of its attributes).
  &lt;p&gt;
  &lt;p&gt;May be applied to an element collection containing instances of an embeddable class or to a map
    collection whose key and/or value is an embeddable class. When &lt;code&gt;AttributeOverride&lt;/code&gt; is
    applied to a map, &lt;i&gt;&quot;key.&quot;&lt;/i&gt; or &lt;i&gt;&quot;value.&quot;&lt;/i&gt; must be used to prefix the name of the
    attribute that is being overridden in order to specify it as part of the map key or map value.
  &lt;/p&gt;
  &lt;p&gt;To override mappings at multiple levels of embedding, a dot (&quot;.&quot;) notation form must be used in
    the name element to indicate an attribute within an embedded attribute. The value of each
    identifier used with the dot notation is the name of the respective embedded field or property.
    If &lt;code&gt;AttributeOverride&lt;/code&gt; is not specified, the column is mapped the same as in the
    original mapping.&lt;/p&gt;
  &lt;h1 style=&quot;color:blue&quot;&gt;@AttributeOverrides annotation&lt;/h1&gt;
  &lt;p&gt;Used to override mappings of multiple properties or fields.&lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Understand using example&lt;/h2&gt;
  &lt;p&gt;We strongly recommend to read these article before moving ahead, &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/06/hibernate-component-mapping.html&quot;
      target=&quot;_blank&quot;&gt;component mapping in
    hibernate using JPA annotation&lt;/a&gt;, &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/06/hibernate-component-mapping.html&quot;
      target=&quot;_blank&quot;&gt;this post&lt;/a&gt;, and &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/06/internally-default-mapping-of-table-and-column.html&quot;
      target=&quot;_blank&quot;&gt;how mapping of column happens when mapping file and @Column annotation is not
    used.&lt;/a&gt;.&lt;/p&gt;
  &lt;p&gt;If we want to change the column names of component address to something like &quot;address_street&quot;,
    &quot;address_city&quot;, and &quot;address_zipcode&quot; as table below.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6XB3dKrtb2UXxrcPHqOLFxWhPNrx8h2IUqjrTkbD3hgT_wIirXypIO6RTH3tXJGwZb54kyY-yelMUIewtmlh56nsETJqVSPJa0HNM6nxki2TxcIFiIEANy0vNKuZ-iylavLqBKYubeuM/s1600/custom_column_name.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6XB3dKrtb2UXxrcPHqOLFxWhPNrx8h2IUqjrTkbD3hgT_wIirXypIO6RTH3tXJGwZb54kyY-yelMUIewtmlh56nsETJqVSPJa0HNM6nxki2TxcIFiIEANy0vNKuZ-iylavLqBKYubeuM/s1600/custom_column_name.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Custom column names of address component&quot;/&gt;&lt;/a&gt;
  &lt;p&gt;We can do the same by using the name attribute of &lt;code&gt;@Column&lt;/code&gt; annotation.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Address.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {

  @Column(name = &amp;quot;address_street&amp;quot;)
  private String street;
  @Column(name = &amp;quot;address_city&amp;quot;)
  private String city;
  @Column(name = &amp;quot;address_zipcode&amp;quot;)
  private String zipcode;

  public Address() {
  }

  public Address(String street, String city, String zipcode) {
    this.street = street;
    this.city = city;
    this.zipcode = zipcode;
  }
}&lt;/pre&gt;
  &lt;p&gt;But sometimes, We need to override the settings of component class into the particular entity
    class. For example, Here by default attributes of address component class will be mapped to
    street, city, and zipcode columns. But, if you wanted to map these columns with some
    other name for this &quot;Person&quot; entity. You can do it using @AttributeOverride annotation.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Person.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = &amp;quot;Person&amp;quot;)
@Getter
@Setter
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = &amp;quot;id&amp;quot;)
  private Long id;

  @Column(name = &amp;quot;name&amp;quot;, nullable = false)
  private String name;

  @Embedded
  @AttributeOverrides({
      @AttributeOverride(name = &amp;quot;street&amp;quot;, column = @Column(name = &amp;quot;address_street&amp;quot;)),
      @AttributeOverride(name = &amp;quot;city&amp;quot;, column = @Column(name = &amp;quot;address_city&amp;quot;)),
      @AttributeOverride(name = &amp;quot;zipcode&amp;quot;, column = @Column(name = &amp;quot;address_zipcode&amp;quot;))
  })
  private Address address;

  public Person() {
  }

  public Person(String name, Address address) {
    this.name = name;
    this.address = address;
  }
}&lt;/pre&gt;
  &lt;p&gt;In &quot;Person&quot; entity, street attribute mapped to &quot;address_street&quot; column, city attribute mapped
    to &quot;address_city&quot; column, and zipcode attribute mapped to &quot;address_zipcode&quot; column.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Address.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.Embeddable;

@Embeddable
public class Address {

  private String street;
  private String city;
  private String zipcode;

  public Address() {
  }

  public Address(String street, String city, String zipcode) {
    this.street = street;
    this.city = city;
    this.zipcode = zipcode;
  }
}&lt;/pre&gt;
  &lt;p&gt;When requirement comes like &quot;Person&quot; entity should have two address say &quot;homeAddress&quot; and
    &quot;billingAddress&quot;. We can achieve the same using the @AttributeOverride annotation. The &quot;Address&quot;
    class is embeddable as above. So it is a &lt;a
        href=&quot;https://walkingtechie.blogspot.com/2020/06/hibernate-entity-types-and-value-types.html&quot;
        target=&quot;_blank&quot;&gt;value type&lt;/a&gt;.&lt;/p&gt;
  &lt;p&gt;The &quot;Person&quot; entity will be like below.&lt;/p&gt;


  &lt;pre class=&quot;brush: java; title:&#39;Person.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = &amp;quot;Person&amp;quot;)
@Getter
@Setter
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = &amp;quot;id&amp;quot;)
  private Long id;

  @Column(name = &amp;quot;name&amp;quot;, nullable = false)
  private String name;

  @Embedded
  @AttributeOverrides({
      @AttributeOverride(name = &amp;quot;street&amp;quot;, column = @Column(name = &amp;quot;home_street&amp;quot;)),
      @AttributeOverride(name = &amp;quot;city&amp;quot;, column = @Column(name = &amp;quot;home_city&amp;quot;)),
      @AttributeOverride(name = &amp;quot;zipcode&amp;quot;, column = @Column(name = &amp;quot;home_zipcode&amp;quot;))
  })
  private Address homeAddress;

  @Embedded
  @AttributeOverrides({
      @AttributeOverride(name = &amp;quot;street&amp;quot;, column = @Column(name = &amp;quot;billing_street&amp;quot;)),
      @AttributeOverride(name = &amp;quot;city&amp;quot;, column = @Column(name = &amp;quot;billing_city&amp;quot;)),
      @AttributeOverride(name = &amp;quot;zipcode&amp;quot;, column = @Column(name = &amp;quot;billing_zipcode&amp;quot;))
  })
  private Address billingAddress;

  public Person() {
  }

  public Person(String name, Address homeAddress, Address billingAddress) {
    this.name = name;
    this.homeAddress = homeAddress;
    this.billingAddress = billingAddress;
  }
}&lt;/pre&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;AttributeOverrideClient&lt;/h2&gt;
  &lt;pre class=&quot;brush: java; title:&#39;AttributeOverrideClient.java&#39;&quot;&gt;package com.walking.techie;

import com.walking.techie.entity.Address;
import com.walking.techie.entity.Person;
import com.walking.techie.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class AttributeOverrideClient {

  public static void main(String[] args) {
    // get session factory of an application
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    // Open a session
    Session session = sessionFactory.openSession();
    Transaction transaction = null;

    try {
      // Begin a unit of work and return the associated Transaction object.
      transaction = session.beginTransaction();

      Address homeAddress = new Address(&amp;quot;6th cross, 6th main&amp;quot;, &amp;quot;Bangalore&amp;quot;, &amp;quot;560078&amp;quot;);
      Address billingAddress = new Address(&amp;quot;11th cross, 7th main&amp;quot;, &amp;quot;New Delhi&amp;quot;, &amp;quot;110005&amp;quot;);
      Person person = new Person(&amp;quot;Santosh&amp;quot;, homeAddress, billingAddress);

      session.persist(person);

      // commit the transaction
      transaction.commit();
    } catch (Exception e) {
      if (transaction != null) {
        transaction.rollback();
        e.printStackTrace();
      }
    } finally {
      if (session != null) {
        // End the session by releasing the JDBC connection and cleaning up.
        session.close();
      }
    }
  }
}&lt;/pre&gt;
  &lt;p&gt;After running the code. The data in &quot;Person&quot; table will be like below.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsvseg_Ai220oyTpCDS2VfeB3DOEDRJZnCbASpspV8h_GhCWUdLJ0hq4YaaNAvAyYk-y46CXRt0kitmbthF55j-zmbT4fOmAmpwVn5f5uDMnLmERq2J52IUAzo7opwi3QTrqS7Y5vQfMI/s1600/home_and_billing_address_table_record.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsvseg_Ai220oyTpCDS2VfeB3DOEDRJZnCbASpspV8h_GhCWUdLJ0hq4YaaNAvAyYk-y46CXRt0kitmbthF55j-zmbT4fOmAmpwVn5f5uDMnLmERq2J52IUAzo7opwi3QTrqS7Y5vQfMI/s1600/home_and_billing_address_table_record.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;@AttributeOverride example table record&quot;/&gt;&lt;/a&gt;
  &lt;p&gt;Please find the working code from &lt;a href=&quot;https://github.com/walkingtechie/attribute-override-jpa&quot; target=&quot;_blank&quot;&gt;git&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/3210416001972098449/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/attributeoverride.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/3210416001972098449'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/3210416001972098449'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/attributeoverride.html' title='@AttributeOverride and @AttributeOverrides example'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6XB3dKrtb2UXxrcPHqOLFxWhPNrx8h2IUqjrTkbD3hgT_wIirXypIO6RTH3tXJGwZb54kyY-yelMUIewtmlh56nsETJqVSPJa0HNM6nxki2TxcIFiIEANy0vNKuZ-iylavLqBKYubeuM/s72-c/custom_column_name.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-2024687970362011062</id><published>2020-06-03T19:31:00.000+05:30</published><updated>2020-06-03T19:45:39.694+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="JPA"/><title type='text'>Internally default mapping of Table and Column in Hibernate</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;p&gt;In last article, we have discussed about &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/06/hibernate-component-mapping.html&quot;
      target=&quot;_blank&quot;&gt;component mapping in
    hibernate using JPA annotation&lt;/a&gt;. Strongly recommend to read &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/06/hibernate-component-mapping.html&quot;
      target=&quot;_blank&quot;&gt;this post&lt;/a&gt;.&lt;/p&gt;
  &lt;p&gt;Lets start the discussion about mapping of columns with the object attributes (member
    variables).&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfVcYPW6aCfjg5ZYc7vCcuWJHa6xze48werwihbPPUAtzW1BTlIl989T1GEQsmX85m5S13JfEDzsE321w5a2KVJ0gDAcToGxrAOBrjPIiHygqltnjbFGaseUXEJh420EwH2UJ_TYSvf3Y/s1600/data-component-mapping.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfVcYPW6aCfjg5ZYc7vCcuWJHa6xze48werwihbPPUAtzW1BTlIl989T1GEQsmX85m5S13JfEDzsE321w5a2KVJ0gDAcToGxrAOBrjPIiHygqltnjbFGaseUXEJh420EwH2UJ_TYSvf3Y/s1600/data-component-mapping.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Table record&quot;/&gt;&lt;/a&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Person.java&#39;&quot;&gt;@Entity
@Getter
@Setter
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(nullable = false)
  private String name;

  @Embedded
  private Address address;

  public Person() {
  }

  public Person(String name, Address address) {
    this.name = name;
    this.address = address;
  }
}&lt;/pre&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Address.java&#39;&quot;&gt;@Embeddable
public class Address {

  private String street;
  private String city;
  private String zipcode;

  public Address() {
  }

  public Address(String street, String city, String zipcode) {
    this.street = street;
    this.city = city;
    this.zipcode = zipcode;
  }
}&lt;/pre&gt;
  &lt;p&gt;The table is automatic create by hibernate because of
    &lt;code&gt;hbm2ddl.auto=create-drop&lt;/code&gt; properties set into hibernate.cfg.xml file. But
    we have nowhere defined the name of the table and column of the table. Where did this &lt;b&gt;&quot;Person&quot;&lt;/b&gt;
    table come from and columns of this table?&lt;/p&gt;
  &lt;p&gt;We have not written the schema for it, no mapping file and not used &lt;code&gt;@Table&lt;/code&gt; JPA
    annotations for mapping. Even we have not used &lt;code&gt;@Column&lt;/code&gt; annotation or mapping file
    to map &lt;b&gt;id&lt;/b&gt; attribute to &lt;b&gt;id&lt;/b&gt; column of this table. Similarly for the rest of the
    attributes. Hibernate automatically figure out about it. First hibernate uses the reasonable
    default values for xml based mapping metadata and also for annotations based mapping metadata.
  &lt;/p&gt;
  &lt;p&gt;So when it finds a mapping information missing. Then it uses sensible default value for
    that.&lt;/p&gt;
  &lt;p&gt;When It doesn&#39;t find the &lt;code&gt;@Table&lt;/code&gt; annotation on entity. It is going to use the name
    of entity as table name. So here name of the entity is &lt;code&gt;&quot;Person&quot;&lt;/code&gt; so it is mapped to
    the name of the table i.e &quot;Person&quot;.&lt;/p&gt;
  &lt;p&gt;Similarly, It doesn&#39;t find the &lt;code&gt;@Column&lt;/code&gt; annotation on an attribute. It is going to
    use the attribute name as column name of the table.&lt;/p&gt;
  &lt;p&gt;Hibernate internally treats them like below:&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Person.java&#39;; highlight:&#39;2,9,12&#39;&quot;&gt;@Entity
@Table(name = &amp;quot;Person&amp;quot;)
@Getter
@Setter
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = &amp;quot;id&amp;quot;)
  private Long id;

  @Column(name = &amp;quot;name&amp;quot;, nullable = false)
  private String name;

  @Embedded
  private Address address;

  public Person() {
  }

  public Person(String name, Address address) {
    this.name = name;
    this.address = address;
  }
}&lt;/pre&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Address.java&#39;; highlight:&#39;4,6,8&#39;&quot;&gt;@Embeddable
public class Address {

  @Column(name = &amp;quot;street&amp;quot;)
  private String street;
  @Column(name = &amp;quot;city&amp;quot;)
  private String city;
  @Column(name = &amp;quot;zipcode&amp;quot;)
  private String zipcode;

  public Address() {
  }

  public Address(String street, String city, String zipcode) {
    this.street = street;
    this.city = city;
    this.zipcode = zipcode;
  }
}&lt;/pre&gt;
  &lt;p&gt;When we run the application that time It create the DDl script using the mapping metadata of
    the person entity and execute the schema against the &quot;helloworld&quot; database.&lt;/p&gt;
  &lt;pre class=&quot;brush: java;&quot;&gt;create table Person (id bigint not null auto_increment, city varchar(255), 
street varchar(255), zipcode varchar(255), name varchar(255) not null, 
primary key (id)) engine=InnoDB&lt;/pre&gt;
  &lt;p&gt;The &lt;b&gt;name&lt;/b&gt; column of the &lt;b&gt;Person&lt;/b&gt; table is having &lt;code&gt;not null&lt;/code&gt; constraint
    that is coming because of &lt;code&gt;@Column(&lt;u
        style=&quot;text-decoration-line: underline; text-decoration-color: green;&quot;&gt;nullable=false&lt;/u&gt;)&lt;/code&gt;.
  &lt;/p&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/2024687970362011062/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/internally-default-mapping-of-table-and-column.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2024687970362011062'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2024687970362011062'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/internally-default-mapping-of-table-and-column.html' title='Internally default mapping of Table and Column in Hibernate'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfVcYPW6aCfjg5ZYc7vCcuWJHa6xze48werwihbPPUAtzW1BTlIl989T1GEQsmX85m5S13JfEDzsE321w5a2KVJ0gDAcToGxrAOBrjPIiHygqltnjbFGaseUXEJh420EwH2UJ_TYSvf3Y/s72-c/data-component-mapping.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-426008239243323590</id><published>2020-06-03T17:20:00.001+05:30</published><updated>2020-06-03T17:32:18.325+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="JPA"/><title type='text'>Hibernate Component Mapping</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;p&gt;A component is a &lt;b&gt;part&lt;/b&gt; of &lt;b&gt;whole&lt;/b&gt; in such a way that if component is destroyed, all
    its &lt;b&gt;parts&lt;/b&gt; are also destroyed with it.&lt;/p&gt;
  &lt;p&gt;For example, Here the house has its rooms as part and the house itself as a whole. When the
    house is destroyed its rooms are also destroyed. Each part (component) belongs
    to only one whole. It means it is not shared with others just like a room in a house. Room is
    the part of your house, not your neighbors.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6Wcir746A5kgTbAMpZVb0zNOYGsH_7tEE7FMConLwmcjfD0MY63a47jjCrYzrXLyECK8X6WqWR2_NBYE_6q5bEg3gUhSzeezkJsvfoidWuD9W2bP8Ga4F3o9v43Jjqe87mm4XqepCYYY/s1600/composition.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6Wcir746A5kgTbAMpZVb0zNOYGsH_7tEE7FMConLwmcjfD0MY63a47jjCrYzrXLyECK8X6WqWR2_NBYE_6q5bEg3gUhSzeezkJsvfoidWuD9W2bP8Ga4F3o9v43Jjqe87mm4XqepCYYY/s1600/composition.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Composition relationship&quot;/&gt;&lt;/a&gt;
  &lt;p&gt;The term &quot;component&quot; refers to the object-oriented notion of &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2020/06/aggregation-and-composition.html&quot;
      target=&quot;_blank&quot;&gt;composition&lt;/a&gt;. It is a embedded object that is persisted as a value type. So
    its existence depend only on the &lt;a
        href=&quot;https://walkingtechie.blogspot.com/2020/06/hibernate-entity-types-and-value-types.html&quot;
        target=&quot;_blank&quot;&gt;entity type&lt;/a&gt; that contains it. Because component does not have its own
    identity in database.
  &lt;/p&gt;
  &lt;p&gt;Let&#39;s take one example of &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Address&lt;/code&gt; object model. Here
    &lt;code&gt;Address&lt;/code&gt; is a component (HAS-A relation) of &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQe4PdLwOg979D8Bkzz031SV7P6YA5q9SgfBLiLUFQRUgy40_42QQsM4t8ud_rEwgjE9gefeyqRNpa6La1aFoC-dNSNNreWeS2EzGlHjLnfCgas6uyVc5986C8NJW97oKGPP_6eDo_Gl0/s1600/component_mapping.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQe4PdLwOg979D8Bkzz031SV7P6YA5q9SgfBLiLUFQRUgy40_42QQsM4t8ud_rEwgjE9gefeyqRNpa6La1aFoC-dNSNNreWeS2EzGlHjLnfCgas6uyVc5986C8NJW97oKGPP_6eDo_Gl0/s1600/component_mapping.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Person and Address mapping&quot;/&gt;&lt;/a&gt;
  &lt;p&gt;Address class is a component of Person class. In this situation, You will have more classes
    than
    the number of corresponding database&#39;s tables to store data of them.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUiKObi281-6Zl4AHwIlU0Hs2etJg9I0vnz6nuY2fYQkL-RE1JLPg9W_kAXmuDU89WHVsiULmIE9GqwLubzKRIvh8dkDa9ecrAkVDmKnecxa_9jhLRM6H9ekrEowhhjTgl8echgpXCQO0/s1600/component_mapping_into_a_table.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUiKObi281-6Zl4AHwIlU0Hs2etJg9I0vnz6nuY2fYQkL-RE1JLPg9W_kAXmuDU89WHVsiULmIE9GqwLubzKRIvh8dkDa9ecrAkVDmKnecxa_9jhLRM6H9ekrEowhhjTgl8echgpXCQO0/s1600/component_mapping_into_a_table.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;component mapping into a table&quot;/&gt;&lt;/a&gt;
  &lt;p&gt;In this example, we have two classes &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Address&lt;/code&gt;, but have
    only one table &lt;b&gt;person&lt;/b&gt; to store data of them. So we are mapping to classes to one table.
  &lt;pre class=&quot;brush: java; title:&#39;Person.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(nullable = false)
  private String name;

  @Embedded
  private Address address;

  public Person() {
  }

  public Person(String name, Address address) {
    this.name = name;
    this.address = address;
  }
}&lt;/pre&gt;
  &lt;p&gt;Person class is an entity class and it has address member variable. To make &lt;b&gt;address&lt;/b&gt;
    attribute as a component of Person class. We mark it with &lt;code&gt;@Embedded&lt;/code&gt; annotation.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Address.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.Embeddable;

@Embeddable
public class Address {

  private String street;
  private String city;
  private String zipcode;

  public Address() {
  }

  public Address(String street, String city, String zipcode) {
    this.street = street;
    this.city = city;
    this.zipcode = zipcode;
  }
}&lt;/pre&gt;
  &lt;p&gt;Here, &lt;code&gt;Address&lt;/code&gt; class is a component, that is a dependent object. We have mark it
    with &lt;code&gt;@Embeddable&lt;/code&gt; annotation to make it a persistent class.&lt;/p&gt;
  &lt;p&gt;Data of both the class will save into one table like below.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfVcYPW6aCfjg5ZYc7vCcuWJHa6xze48werwihbPPUAtzW1BTlIl989T1GEQsmX85m5S13JfEDzsE321w5a2KVJ0gDAcToGxrAOBrjPIiHygqltnjbFGaseUXEJh420EwH2UJ_TYSvf3Y/s1600/data-component-mapping.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfVcYPW6aCfjg5ZYc7vCcuWJHa6xze48werwihbPPUAtzW1BTlIl989T1GEQsmX85m5S13JfEDzsE321w5a2KVJ0gDAcToGxrAOBrjPIiHygqltnjbFGaseUXEJh420EwH2UJ_TYSvf3Y/s1600/data-component-mapping.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Table record&quot;/&gt;&lt;/a&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Hibernate Properties&lt;/h2&gt;
  &lt;p&gt;&lt;code&gt;hibernate.cfg.xml&lt;/code&gt; is a Hibernate configuration xml file which contain the required
    Hibernate
    properties.
    Most of the properties have their default properties. Some of the properties values need to have
    into this file,
    so Hibernate use these properties to create connection with database.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;hibernate.cfg.xml&#39;&quot;&gt;&amp;lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?&amp;gt;
&amp;lt;!DOCTYPE hibernate-configuration PUBLIC
  &amp;quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&amp;quot;
  &amp;quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&amp;quot;&amp;gt;

&amp;lt;hibernate-configuration&amp;gt;
  &amp;lt;session-factory&amp;gt;
    &amp;lt;property name=&amp;quot;connection.driver_class&amp;quot;&amp;gt;com.mysql.jdbc.Driver&amp;lt;/property&amp;gt;
    &amp;lt;property name=&amp;quot;connection.url&amp;quot;&amp;gt;jdbc:mysql://localhost:3306/helloworld?useSSL=false&amp;lt;/property&amp;gt;
    &amp;lt;property name=&amp;quot;connection.username&amp;quot;&amp;gt;root&amp;lt;/property&amp;gt;
    &amp;lt;property name=&amp;quot;connection.password&amp;quot;&amp;gt;santosh&amp;lt;/property&amp;gt;
    &amp;lt;property name=&amp;quot;hbm2ddl.auto&amp;quot;&amp;gt;create-drop&amp;lt;/property&amp;gt;

    &amp;lt;!-- SQL Dialect --&amp;gt;
    &amp;lt;property name=&amp;quot;dialect&amp;quot;&amp;gt;org.hibernate.dialect.MariaDBDialect&amp;lt;/property&amp;gt;
    &amp;lt;!--Echo all executed SQL query to console--&amp;gt;
    &amp;lt;property name=&amp;quot;show_sql&amp;quot;&amp;gt;true&amp;lt;/property&amp;gt;
    &amp;lt;mapping class=&amp;quot;com.walking.techie.entity.Person&amp;quot;/&amp;gt;
  &amp;lt;/session-factory&amp;gt;
&amp;lt;/hibernate-configuration&amp;gt;&lt;/pre&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;ComponentMappingClient&lt;/h2&gt;
  &lt;p&gt;This class main method will save the person object into &lt;b&gt;Person&lt;/b&gt; table of
    &lt;b&gt;helloworld&lt;/b&gt; database.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;ComponentMappingClient.java&#39;&quot;&gt;package com.walking.techie;

import com.walking.techie.entity.Address;
import com.walking.techie.entity.Person;
import com.walking.techie.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class ComponentMappingClient {

  public static void main(String[] args) {
    // get session factory of an application
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    // Open a session
    Session session = sessionFactory.openSession();
    Transaction transaction = null;

    try {
      // Begin a unit of work and return the associated Transaction object.
      transaction = session.beginTransaction();

      Address address = new Address(&amp;quot;6th cross, 6th main&amp;quot;, &amp;quot;Bangalore&amp;quot;, &amp;quot;560078&amp;quot;);
      Person person = new Person(&amp;quot;Santosh&amp;quot;, address);

      session.persist(person);

      // commit the transaction
      transaction.commit();
    } catch (Exception e) {
      if (transaction != null) {
        transaction.rollback();
        e.printStackTrace();
      }
    } finally {
      if (session != null) {
        // End the session by releasing the JDBC connection and cleaning up.
        session.close();
      }
    }
  }
}&lt;/pre&gt;
  &lt;p&gt;Before running this application you need to create the &lt;b&gt;helloworld&lt;/b&gt; database into MySQL.
    Table will created
    automatically because we have set the &lt;b&gt;hbm2ddl.auto&lt;/b&gt; to create-drop.&lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Output&lt;/h2&gt;
  &lt;p&gt;This application will save the person object and its component address object in &lt;b&gt;Person&lt;/b&gt;
    table.&lt;/p&gt;
  &lt;p&gt;output in console&lt;/p&gt;
  &lt;pre class=&quot;brush: java;&quot;&gt;Hibernate: create table Person (id bigint not null auto_increment, city varchar(255), street varchar(255), zipcode varchar(255), name varchar(255) not null, primary key (id)) engine=InnoDB
Jun 03, 2020 4:49:03 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@1e0f9063] for (non-JTA) DDL execution was not in auto-commit mode; the Connection &#39;local transaction&#39; will be committed and the Connection will be set into auto-commit mode.
Jun 03, 2020 4:49:04 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script &#39;org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@63a270c9&#39;
Hibernate: insert into Person (city, street, zipcode, name) values (?, ?, ?, ?)
&lt;/pre&gt;
  &lt;p&gt;You can find the working code of above example on &lt;a
      href=&quot;https://github.com/walkingtechie/component-mapping&quot; target=&quot;_blank&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/426008239243323590/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/hibernate-component-mapping.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/426008239243323590'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/426008239243323590'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/hibernate-component-mapping.html' title='Hibernate Component Mapping'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6Wcir746A5kgTbAMpZVb0zNOYGsH_7tEE7FMConLwmcjfD0MY63a47jjCrYzrXLyECK8X6WqWR2_NBYE_6q5bEg3gUhSzeezkJsvfoidWuD9W2bP8Ga4F3o9v43Jjqe87mm4XqepCYYY/s72-c/composition.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-6093682239720915255</id><published>2020-06-03T13:09:00.000+05:30</published><updated>2020-06-03T17:32:32.124+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="JPA"/><title type='text'>Hibernate mapping types and Entity vs Value Types</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;p&gt;In Hibernate, mapping types categorized into two groups. In this article we will see overview
    of Hibernate mapping types and the difference between them.&lt;/p&gt;
  &lt;h1 style=&quot;color:blue&quot;&gt;Hibernate mapping types&lt;/h1&gt;
  &lt;p&gt;Hibernate categorized into two groups with respect to persistence.&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;Entity Type&lt;/li&gt;
    &lt;li&gt;Value Type&lt;/li&gt;
  &lt;/ol&gt;
  &lt;h1 style=&quot;color:blue&quot;&gt;Entity Type&lt;/h1&gt;
  &lt;p&gt;An object of &lt;code&gt;Entity&lt;/code&gt; type has its own database identity means &lt;code&gt;primary
    key&lt;/code&gt; in table.&lt;/p&gt;
  &lt;p&gt;An entity has its own lifecycle. It may exist independently of any other entity.&lt;/p&gt;
  &lt;p&gt;A reference of entity object is persisted as a reference in database (foreign key).&lt;/p&gt;
  &lt;h1 style=&quot;color:blue&quot;&gt;Value Type&lt;/h1&gt;
  &lt;p&gt;An object of &lt;code&gt;Value&lt;/code&gt; type has no database identity and it belongs to entity.&lt;/p&gt;
  &lt;p&gt;The lifecycle of &lt;code&gt;Value&lt;/code&gt; type object is bounds to its owning &lt;code&gt;entity&lt;/code&gt;
    object&#39;s lifecycle.&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;Value&lt;/code&gt; type object is identified by its owning &lt;code&gt;entity&lt;/code&gt;.&lt;/p&gt;
  &lt;p&gt;It is embedded into owning entity and it represented as table&#39;s column in database./p&gt;
  &lt;p&gt;&lt;code&gt;Value&lt;/code&gt; types further divided into three groups.&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;Basic value types&lt;/li&gt;
    &lt;li&gt;Composite types&lt;/li&gt;
    &lt;li&gt;Collection types&lt;/li&gt;
  &lt;/ol&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Basic value types&lt;/h2&gt;
  &lt;ul&gt;
    &lt;li&gt;Basic value types maps a single database value (column) to a single, non-aggregated Java
      type.
    &lt;/li&gt;
    &lt;li&gt;Hibernate provides a number of built-in basic types and all the Java wrapper classes.&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Character&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Byte&lt;/code&gt;, &lt;code&gt;Short&lt;/code&gt;,
      &lt;code&gt;Integer&lt;/code&gt;, &lt;code&gt;Long&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, &lt;code&gt;BigInteger&lt;/code&gt;,
      &lt;code&gt;BigDecimal&lt;/code&gt;, &lt;code&gt;java.util.Date or java.sql.Timestamp&lt;/code&gt;, &lt;code&gt;java.sql.Time&lt;/code&gt;,
      &lt;code&gt;java.sql.Date&lt;/code&gt;, &lt;code&gt;java.util.Calendar&lt;/code&gt;,
      &lt;code&gt;java.util.Currency&lt;/code&gt;, &lt;code&gt;java.util.UUID&lt;/code&gt;, &lt;code&gt;java.lang.Class&lt;/code&gt;,
      &lt;code&gt;java.sql.Blob&lt;/code&gt; .. etc are built-in basic value types.
    &lt;/li&gt;
    &lt;li&gt;You can find the all the built-in basic types from &lt;a
        href=&quot;https://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch06.html#types-value-basic&quot;&gt;Hibernate
      documentation Basic
      value types&lt;/a&gt;.
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Composite types&lt;/h2&gt;
  &lt;ul&gt;
    &lt;li&gt;The Java Persistence API calls these &lt;b&gt;&lt;code&gt;embedded types&lt;/code&gt;&lt;/b&gt;, while Hibernate
      traditionally called them &lt;b&gt;&lt;code&gt;components&lt;/code&gt;&lt;/b&gt;.
    &lt;/li&gt;
    &lt;li&gt;Components represent aggregations of values into a single Java type.&lt;/li&gt;
    &lt;li&gt;In many ways, Composite types looks exactly like an &lt;code&gt;entity&lt;/code&gt;. &lt;code&gt;Composite
      types&lt;/code&gt; does not own its own lifecycle nor does it define an identifier.
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Collection types&lt;/h2&gt;
  &lt;p&gt;Hibernate allows to persist collections. &lt;code&gt;Collections types&lt;/code&gt; further sub categorized
    into three types.&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;Collection of basic value types.&lt;/li&gt;
    &lt;li&gt;Collection of Composite or embeddable types.&lt;/li&gt;
    &lt;li&gt;Collection of custom types.&lt;/li&gt;
  &lt;/ol&gt;
  &lt;br&gt;
  &lt;p&gt;Now we take one example to understand the &lt;code&gt;Entity type&lt;/code&gt;, and &lt;code&gt;Value Type&lt;/code&gt;.
    Below &lt;b&gt;USERS&lt;/b&gt; table has user details, home address, and billing address.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMIFG4FyDXQJEcoDNZLY8NmXwC6RysvgKuM3fiOHjW-VsffNVa9SnFoIqU6-t86356Fx1OjuHMTrtLl-4jzPxpEBJtjEEiOGroE_RkD3sALsV11UlpuDx6Do5FG_UQqME2tUAxKbkPhOs/s1600/user_table.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMIFG4FyDXQJEcoDNZLY8NmXwC6RysvgKuM3fiOHjW-VsffNVa9SnFoIqU6-t86356Fx1OjuHMTrtLl-4jzPxpEBJtjEEiOGroE_RkD3sALsV11UlpuDx6Do5FG_UQqME2tUAxKbkPhOs/s1600/user_table.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=User table diagram&quot;/&gt;&lt;/a&gt;

  &lt;p&gt;Now we a represent the same table in the object model like below with the six-member variables
    homeStreet, homeCity, homePincode, billingStreet, billingCity, and billingPincode.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;User.java&#39;&quot;&gt;public class User {

  private Long id;
  private String name;
  private String homeStreet;
  private String homeCity;
  private String homePincode;
  private String billingStreet;
  private String billingCity;
  private String billingPincode;
}&lt;/pre&gt;
  &lt;p&gt;Rather we would like to have a separate class User and Address. Address class only have address
    related details. The homeAddress and billingAddress member variable of class User to access
    user&#39;s address from it. This is better design from code reuse perspective.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;User.java&#39;&quot;&gt;public class User {

  private Long id;
  private String name;
  private Address homeAddress;
  private Address billingAddress;
}&lt;/pre&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Address.java&#39;&quot;&gt;public class Address {

  private String street;
  private String city;
  private String pincode;
}&lt;/pre&gt;
  &lt;p&gt;The &lt;i&gt;id&lt;/i&gt; attribute in &lt;code&gt;User&lt;/code&gt; class identity the record uniquely in database&#39;s
    table. Here we don&#39;t have &lt;i&gt;id&lt;/i&gt; attribute in &lt;code&gt;Address&lt;/code&gt; class and didn&#39;t have any
    significance of identity of an address.&lt;/p&gt;
  &lt;p&gt;You can follow the article &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2018/01/hello-world-application-with-hibernate.html&quot;
      target=&quot;_blank&quot;&gt;this article to create Hello World application with Hibernate.&lt;/a&gt; and &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2019/05/hello-world-application-with-hibernate-using-jpa-annotations.html&quot;
      target=&quot;_blank&quot;&gt;Hello world application using JPA Annotations&lt;/a&gt;. You can also follow
    &lt;a href=&quot;https://walkingtechie.blogspot.com/2019/06/difference-between-entity-and-table.html&quot;
       target=&quot;_blank&quot;&gt;this
      article to find out the difference between @Entity and @Table annotation&lt;/a&gt;.&lt;/p&gt;
  &lt;p&gt;The objects that have their own database identity are &lt;code&gt;Entity type&lt;/code&gt; object. So here
    &lt;code&gt;User&lt;/code&gt; is an &lt;code&gt;Entity type&lt;/code&gt; and &lt;code&gt;Address&lt;/code&gt; is a &lt;code&gt;Value
      type&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/6093682239720915255/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/hibernate-entity-types-and-value-types.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/6093682239720915255'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/6093682239720915255'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/hibernate-entity-types-and-value-types.html' title='Hibernate mapping types and Entity vs Value Types'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMIFG4FyDXQJEcoDNZLY8NmXwC6RysvgKuM3fiOHjW-VsffNVa9SnFoIqU6-t86356Fx1OjuHMTrtLl-4jzPxpEBJtjEEiOGroE_RkD3sALsV11UlpuDx6Do5FG_UQqME2tUAxKbkPhOs/s72-c/user_table.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-2330128612517441353</id><published>2020-06-01T13:46:00.000+05:30</published><updated>2020-06-03T17:32:43.334+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="JPA"/><title type='text'>Aggregation and Composition</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Aggregation&lt;/h2&gt;
  &lt;p&gt;Aggregation indicates a relationship between whole and its parts.&lt;/p&gt;
  &lt;p&gt;For example, A music band is made up of artists. So music band has its artists as its parts and
    the band is the whole.
    If the band is broken. Its artists does not have to broken with it. They could still join other
    band independently or some of them can even start their own band.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIUrlPzOFJ_NKcVzKmgkZvcEcyrorNQAxleOrR6hP5_JGeMcffuncbAF245wDWEp-yXCQrczp1mEJacGVx1e6oRiGrmNw7-NIU8QAQ3jHLXNT8fjT4eAQXnH3hYK0QaH0tXS4NOCMuLO0/s1600/aggregation.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIUrlPzOFJ_NKcVzKmgkZvcEcyrorNQAxleOrR6hP5_JGeMcffuncbAF245wDWEp-yXCQrczp1mEJacGVx1e6oRiGrmNw7-NIU8QAQ3jHLXNT8fjT4eAQXnH3hYK0QaH0tXS4NOCMuLO0/s1600/aggregation.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Aggregation relationship&quot;/&gt;&lt;/a&gt;
  &lt;p&gt;In aggregation relationship, When the whole is destroyed, its parts are not destroyed with
    it.&lt;/p&gt;

  &lt;h2 style=&quot;color:blue&quot;&gt;Composition&lt;/h2&gt;
  &lt;p&gt;Composition is a strong form of aggregation. When we talk about composition relationship, when the whole is destroyed its parts are also destroyed with it. it is a strong form of aggregation.&lt;/p&gt;
  &lt;p&gt;For example, Here the house has its rooms as part and the house itself as a whole. When the house is destroyed its rooms are also destroyed. In composition relationship each part belongs to only one whole. It means it is not shared with others just like a room in a house. Room is the part of your house, not your neighbors.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6Wcir746A5kgTbAMpZVb0zNOYGsH_7tEE7FMConLwmcjfD0MY63a47jjCrYzrXLyECK8X6WqWR2_NBYE_6q5bEg3gUhSzeezkJsvfoidWuD9W2bP8Ga4F3o9v43Jjqe87mm4XqepCYYY/s1600/composition.png&quot;
     imageanchor=&quot;1&quot;&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6Wcir746A5kgTbAMpZVb0zNOYGsH_7tEE7FMConLwmcjfD0MY63a47jjCrYzrXLyECK8X6WqWR2_NBYE_6q5bEg3gUhSzeezkJsvfoidWuD9W2bP8Ga4F3o9v43Jjqe87mm4XqepCYYY/s1600/composition.png&quot;
         style=&quot;max-width: 100%;height: auto&quot; alt=&quot;Composition relationship&quot;/&gt;&lt;/a&gt;
  &lt;p&gt;Unlike aggregation relationship, when the whole is destroyed its parts are also destroyed with
    it.&lt;/p&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/2330128612517441353/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/aggregation-and-composition.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2330128612517441353'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2330128612517441353'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/06/aggregation-and-composition.html' title='Aggregation and Composition'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIUrlPzOFJ_NKcVzKmgkZvcEcyrorNQAxleOrR6hP5_JGeMcffuncbAF245wDWEp-yXCQrczp1mEJacGVx1e6oRiGrmNw7-NIU8QAQ3jHLXNT8fjT4eAQXnH3hYK0QaH0tXS4NOCMuLO0/s72-c/aggregation.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-3432969091954212264</id><published>2020-01-30T22:03:00.000+05:30</published><updated>2020-01-30T22:04:19.844+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Spring Boot"/><title type='text'>profile based logback in spring boot</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;p&gt;You can print the log in the spring boot based on your deployment enviornment. create &lt;code&gt;logback-spring.xml&lt;/code&gt; file under the resources folder of spring boot project structure.&lt;/p&gt;
&lt;pre class=&quot;brush: java; title:&#39;logback-spring.xml&#39;&quot;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;

&amp;lt;configuration&amp;gt;
    &amp;lt;include resource=&amp;quot;org/springframework/boot/logging/logback/defaults.xml&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;LOG_FILE&amp;quot; value=&amp;quot;${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;LOG_PATH&amp;quot; value=&amp;quot;/home/santosh/logs/some-path&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;LOG_ARCHIVE&amp;quot; value=&amp;quot;${LOG_PATH}/archive&amp;quot;/&amp;gt;
    &amp;lt;timestamp key=&amp;quot;timestamp-by-second&amp;quot; datePattern=&amp;quot;yyyyMMdd&#39;T&#39;HHmmss&amp;quot;/&amp;gt;
    &amp;lt;include resource=&amp;quot;org/springframework/boot/logging/logback/console-appender.xml&amp;quot;/&amp;gt;

    &amp;lt;!----&amp;gt;
    &amp;lt;appender name=&amp;quot;FILE&amp;quot; class=&amp;quot;ch.qos.logback.core.rolling.RollingFileAppender&amp;quot;&amp;gt;
        &amp;lt;encoder&amp;gt;
            &amp;lt;pattern&amp;gt;${FILE_LOG_PATTERN}&amp;lt;/pattern&amp;gt;
        &amp;lt;/encoder&amp;gt;
        &amp;lt;file&amp;gt;${LOG_FILE}&amp;lt;/file&amp;gt;
        &amp;lt;rollingPolicy class=&amp;quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&amp;quot;&amp;gt;
            &amp;lt;fileNamePattern&amp;gt;${LOG_FILE}.%d{yyyy-MM-dd}.%i&amp;lt;/fileNamePattern&amp;gt;
        &amp;lt;/rollingPolicy&amp;gt;
    &amp;lt;/appender&amp;gt;

    &amp;lt;springProfile name=&amp;quot;dev&amp;quot;&amp;gt;
        &amp;lt;root level=&amp;quot;DEBUG&amp;quot;&amp;gt;
            &amp;lt;appender-ref ref=&amp;quot;CONSOLE&amp;quot;/&amp;gt;
        &amp;lt;/root&amp;gt;
    &amp;lt;/springProfile&amp;gt;
    &amp;lt;springProfile name=&amp;quot;stage&amp;quot;&amp;gt;
        &amp;lt;root level=&amp;quot;INFO&amp;quot;&amp;gt;
            &amp;lt;appender-ref ref=&amp;quot;CONSOLE&amp;quot;/&amp;gt;
            &amp;lt;appender-ref ref=&amp;quot;FILE&amp;quot;/&amp;gt;
        &amp;lt;/root&amp;gt;
    &amp;lt;/springProfile&amp;gt;
    &amp;lt;springProfile name=&amp;quot;prod&amp;quot;&amp;gt;
        &amp;lt;root level=&amp;quot;INFO&amp;quot;&amp;gt;
            &amp;lt;appender-ref ref=&amp;quot;FILE&amp;quot;/&amp;gt;
        &amp;lt;/root&amp;gt;
    &amp;lt;/springProfile&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/3432969091954212264/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2020/01/profile-based-logback-in-spring-boot.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/3432969091954212264'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/3432969091954212264'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2020/01/profile-based-logback-in-spring-boot.html' title='profile based logback in spring boot'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-1835856154540969193</id><published>2019-06-02T18:23:00.000+05:30</published><updated>2020-06-03T17:33:00.943+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="JPA"/><title type='text'>Difference between Entity and Table annotation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;p&gt;
    &lt;code&gt;@javax.persistence.Entity&lt;/code&gt;:Specifies that the class is an entity. This annotation is
    applied to the entity class.&lt;br/&gt;
    &lt;code&gt;@javax.persistence.Table&lt;/code&gt;:Specifies the primary table for the annotated entity.
    Additional tables may be specified using SecondaryTable or SecondaryTables annotation.
    If no Table annotation is specified for an entity class, the default values apply.&lt;br/&gt;
  &lt;/p&gt;
  &lt;h1 style=&quot;color: blue&quot;&gt;Difference between @Entity and @Table&lt;/h1&gt;
  &lt;ol&gt;
    &lt;li&gt;&lt;code&gt;@Entity(name = &quot;someName&quot;)&lt;/code&gt; : this name will be used to name the Entity&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;@Table(name = &quot;otherName&quot;)&lt;/code&gt; : this name will be used to name a table in DB&lt;/li&gt;
  &lt;/ol&gt;
  There will be only two valid scenario. Let&#39;s understand this with examples.
  We will using persistence &lt;code&gt;Message&lt;/code&gt; class through out this discussions.
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Entity
public class Message {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = &amp;quot;ID&amp;quot;)
  private Long id;

  @Column(name = &amp;quot;MESSAGE&amp;quot;)
  private String message;

  public Message(String message) {
    this.message = message;
  }
}&lt;/pre&gt;
  &lt;p&gt;We will have both properties &lt;code&gt;&amp;lt;property name=&amp;quot;hbm2ddl.auto&amp;quot;&amp;gt;create-drop&amp;lt;/property&amp;gt;&lt;/code&gt;
    and &lt;code&gt;&amp;lt;property name=&amp;quot;show_sql&amp;quot;&amp;gt;true&amp;lt;/property&amp;gt;&lt;/code&gt; in &lt;code&gt;hibernate.cfg.xml&lt;/code&gt;
    file.
    You can refer this from &lt;a
        href=&quot;https://walkingtechie.blogspot.com/2019/05/hello-world-application-with-hibernate-using-jpa-annotations.html&quot;
        target=&quot;_blank&quot;&gt;here.&lt;/a&gt; So that every time we run Hibernate application table will deleted
    if it exists in DB and create new one in DB and sql queries will print on console.&lt;/p&gt;
  &lt;h2 style=&quot;color: blue&quot;&gt;Scenario 1: Only @Entity annotation used&lt;/h2&gt;
  &lt;h3 style=&quot;color: #3b5998&quot;&gt;Scenario 1.1: When only @Entity annotation used on persistence
    class&lt;/h3&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;@Entity
public class Message {
    .............
    .............
}&lt;/pre&gt;
  &lt;p&gt;If you don&#39;t use name element then table and entity name will be same as default; name of
    persistence class.&lt;/p&gt;
  &lt;i&gt;Hibernate Query:&lt;/i&gt; &lt;b&gt;create table Message (ID bigint not null auto_increment, MESSAGE
  varchar(255), primary key (ID)) engine=InnoDB&lt;/b&gt;
  &lt;h3 style=&quot;color: #3b5998&quot;&gt;Scenario 1.2: When @Entity annotation used with name element on
    persistence class&lt;/h3&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;@Entity(name = &amp;quot;message&amp;quot;)
public class Message {
    .............
    .............
}&lt;/pre&gt;
  &lt;p&gt;If you use name element then table and entity name will be same as value of name
    element.&lt;/p&gt;
  &lt;i&gt;Hibernate Query:&lt;/i&gt; &lt;b&gt;create table message (ID bigint not null auto_increment, MESSAGE
  varchar(255), primary key (ID)) engine=InnoDB&lt;/b&gt;
  &lt;p&gt;In this case table and entity will have the same name, that will allow you to
    access your table with the same name as the entity while writing HQL or JPQL.&lt;/p&gt;
  &lt;h2 style=&quot;color: blue&quot;&gt;Scenario 2: Both @Entity and @Table annotations used&lt;/h2&gt;
  &lt;h3 style=&quot;color: #3b5998&quot;&gt;Scenario 2.1: @Entity and @Table annotations used on persistence
    class&lt;/h3&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;@Entity
@Table
public class Message {
    .............
    .............
}&lt;/pre&gt;
  &lt;p&gt;This is same as scenario 1.1.&lt;/p&gt;
  &lt;i&gt;Hibernate Query:&lt;/i&gt; &lt;b&gt;create table Message (ID bigint not null auto_increment, MESSAGE
  varchar(255), primary key (ID)) engine=InnoDB&lt;/b&gt;
  &lt;h3 style=&quot;color: #3b5998&quot;&gt;Scenario 2.2: @Entity(name = &amp;quot;MESSAGE&amp;quot;) and @Table
    annotations used on persistence class&lt;/h3&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;@Entity(name = &amp;quot;MESSAGE&amp;quot;)
@Table
public class Message {
    .............
    .............
}&lt;/pre&gt;
  &lt;i&gt;Hibernate Query:&lt;/i&gt; &lt;b&gt;create table MESSAGE (ID bigint not null auto_increment, MESSAGE
  varchar(255), primary key (ID)) engine=InnoDB&lt;/b&gt;
  &lt;h3 style=&quot;color: #3b5998&quot;&gt;Scenario 2.3: @Entity and @Table(name = &amp;quot;text_message&amp;quot;)
    annotations used on persistence class&lt;/h3&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;@Entity
@Table(name = &amp;quot;text_message&amp;quot;)
public class Message {
    .............
    .............
}&lt;/pre&gt;
  &lt;i&gt;Hibernate Query:&lt;/i&gt; &lt;b&gt;create table text_message (ID bigint not null auto_increment, MESSAGE
  varchar(255), primary key (ID)) engine=InnoDB&lt;/b&gt;
  &lt;h3 style=&quot;color: #3b5998&quot;&gt;Scenario 2.4: @Entity(name = &amp;quot;MESSAGE&amp;quot;) and @Table(name =
    &amp;quot;text_message&amp;quot;) annotations
    used on persistence class&lt;/h3&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;@Entity(name = &amp;quot;MESSAGE&amp;quot;)
@Table(name = &amp;quot;text_message&amp;quot;)
public class Message {
    .............
    .............
}&lt;/pre&gt;
  &lt;i&gt;Hibernate Query:&lt;/i&gt; &lt;b&gt;create table text_message (ID bigint not null auto_increment, MESSAGE
  varchar(255), primary key (ID)) engine=InnoDB&lt;/b&gt;
  &lt;p&gt;In this case, while writing queries you have to use the name given in @Entity and the name
    given in @Table will be used to name the table in the DB. So the entity name you use in your
    JPQL will refer to &lt;code&gt;otherName&lt;/code&gt; in the DB.&lt;/p&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/1835856154540969193/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2019/06/difference-between-entity-and-table.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/1835856154540969193'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/1835856154540969193'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2019/06/difference-between-entity-and-table.html' title='Difference between Entity and Table annotation'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-8794678325045318964</id><published>2019-05-30T16:42:00.000+05:30</published><updated>2019-05-30T16:43:20.120+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="hibernate"/><title type='text'>Hello World application with Hibernate using JPA annotations</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
  &lt;p&gt;Welcome to the Hibernate tutorial from beginner to advance. Hibernate is one of the most
    popular ORM tool. We will discuss each and every concept in detail. You will find this post very
    useful even you are beginner or advance in Hibernate.&lt;/p&gt;
  &lt;p&gt;We will understand the basic concept of Hibernate using the &lt;b&gt;Hello World program with JPA
    annotations&lt;/b&gt; that
    will be very simple hibernate program, will explain about hibernate concept.&lt;/p&gt;
  &lt;p&gt;In the previous post, &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2018/01/hello-world-application-with-hibernate.html&quot;&gt;
    we have discussed about the Hello World program using the mapping file.&lt;/a&gt;&lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Hello World Program with JPA annotations&lt;/h2&gt;
  &lt;p&gt;We will create the hello-world-hibernate-jpa-anno gradle based application. Project structure
    of this application is something like below.&lt;/p&gt;
  &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2rDdKFRtmtfiXBw_bs0B5WymHjBhiRJOtFnFXSl6KPe3byc99sa6s5fyg8ci_d8FYBCrDj4ZFndUIeywwioHoSziaD_x3hu2yVhn0-C13yRUvQdo6Xu8FXCGikFaX3LHEY11-0_sQK0o/s1600/hello-world-application-using-jpa-annotation.png&quot;
     imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot;
                          src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2rDdKFRtmtfiXBw_bs0B5WymHjBhiRJOtFnFXSl6KPe3byc99sa6s5fyg8ci_d8FYBCrDj4ZFndUIeywwioHoSziaD_x3hu2yVhn0-C13yRUvQdo6Xu8FXCGikFaX3LHEY11-0_sQK0o/s320/hello-world-application-using-jpa-annotation.png&quot;
                          style=&quot;max-width: 100%;height: auto&quot;
                          alt=&quot;Hello World with Hibernate using JPA annotations Project Structure&quot;/&gt;&lt;/a&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Hibernate Properties&lt;/h2&gt;
  &lt;p&gt;&lt;code&gt;hibernate.cfg.xml&lt;/code&gt; is a Hibernate configuration xml file which contain the
    required Hibernate properties. Most of the properties have their own default value. Some of the
    properties values need to have into this file, so Hibernate use these properties to create
    connection with database.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;hibernate.cfg.xml&#39;&quot;&gt;&amp;lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?&amp;gt;
&amp;lt;!DOCTYPE hibernate-configuration PUBLIC
        &amp;quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&amp;quot;
        &amp;quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&amp;quot;&amp;gt;

&amp;lt;hibernate-configuration&amp;gt;
    &amp;lt;session-factory&amp;gt;
        &amp;lt;property name=&amp;quot;connection.driver_class&amp;quot;&amp;gt;com.mysql.jdbc.Driver&amp;lt;/property&amp;gt;
        &amp;lt;property name=&amp;quot;connection.url&amp;quot;&amp;gt;jdbc:mysql://localhost:3306/helloworld?useSSL=false&amp;lt;/property&amp;gt;
        &amp;lt;property name=&amp;quot;connection.username&amp;quot;&amp;gt;root&amp;lt;/property&amp;gt;
        &amp;lt;property name=&amp;quot;connection.password&amp;quot;&amp;gt;santosh&amp;lt;/property&amp;gt;
        &amp;lt;property name=&amp;quot;hbm2ddl.auto&amp;quot;&amp;gt;update&amp;lt;/property&amp;gt;

        &amp;lt;!-- SQL Dialect --&amp;gt;
        &amp;lt;property name=&amp;quot;dialect&amp;quot;&amp;gt;org.hibernate.dialect.MariaDBDialect&amp;lt;/property&amp;gt;
        &amp;lt;!--Echo all executed SQL query to console--&amp;gt;
        &amp;lt;property name=&amp;quot;show_sql&amp;quot;&amp;gt;true&amp;lt;/property&amp;gt;
        &amp;lt;mapping class=&amp;quot;com.walking.techie.entity.Message&amp;quot;/&amp;gt;

    &amp;lt;/session-factory&amp;gt;
&amp;lt;/hibernate-configuration&amp;gt;&lt;/pre&gt;
  &lt;p&gt;This table show some of the important properties that you will use into Hibernate
    configuration file.&lt;/p&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;th&gt;Properties&lt;/th&gt;
      &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;connection.driver_class&lt;/b&gt;&lt;/td&gt;
      &lt;td&gt;The JDBC driver class.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;connection.url&lt;/b&gt;&lt;/td&gt;
      &lt;td&gt;The JDBC URL to the database instance.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;connection.username&lt;/b&gt;&lt;/td&gt;
      &lt;td&gt;The database username to authenticate.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;connection.password&lt;/b&gt;&lt;/td&gt;
      &lt;td&gt;The database password to authenticate.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;hbm2ddl.auto&lt;/b&gt;&lt;/td&gt;
      &lt;td&gt;This property validates or exports schema DDL to the database when the SessionFactory is
        created.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;dialect&lt;/b&gt;&lt;/td&gt;
      &lt;td&gt;This property makes Hibernate to generate the appropriate SQL for the chosen database.
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;show_sql&lt;/b&gt;&lt;/td&gt;
      &lt;td&gt;This property echo the generated SQL query on console.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;br/&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Create SessionFactory&lt;/h2&gt;
  &lt;p&gt;Usually an application has single &lt;code&gt;SessionFactory&lt;/code&gt; instance and &lt;code&gt;Session&lt;/code&gt;
    instance can get from this factory instance. Client request served by threads using &lt;code&gt;Session&lt;/code&gt;
    instance.
  &lt;/p&gt;
  &lt;p&gt;The internal state of a &lt;code&gt;SessionFactory&lt;/code&gt; is immutable. Once it is created this
    internal state is set.
    This internal state includes all of the metadata about Object/Relational Mapping.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;HibernateUtil.java&#39;&quot;&gt;package com.walking.techie.utils;


import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil {

    public static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                    .configure(&amp;quot;hibernate.cfg.xml&amp;quot;).build();
            Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
            return metadata.getSessionFactoryBuilder().build();
        } catch (Exception ex) {
            System.err.println(&amp;quot;Initial session factory creation failed: &amp;quot; + ex.getMessage());
            throw new ExceptionInInitializerError();
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}&lt;/pre&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Model&lt;/h2&gt;
  &lt;p&gt;&lt;code&gt;Message&lt;/code&gt; Java class is a persistence entity that will save into relational
    database. I have used the
    lombok annotation here to create getter, setter, default constructor of the Message class.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;Message.java&#39;&quot;&gt;package com.walking.techie.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Entity
@Table(name = &amp;quot;MESSAGE&amp;quot;)
public class Message {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = &amp;quot;ID&amp;quot;)
  private Long id;

  @Column(name = &amp;quot;MESSAGE&amp;quot;)
  private String message;

  public Message(String message) {
    this.message = message;
  }
}&lt;/pre&gt;
  &lt;p&gt;In the previous post, we have used &lt;a
      href=&quot;https://walkingtechie.blogspot.com/2018/01/hello-world-application-with-hibernate.html&quot;&gt;XML
    based mapping file&lt;/a&gt; to specify mapping between
    &lt;code&gt;Message&lt;/code&gt; class and &amp;quot;MESSAGE&amp;quot; table. In XML based metadata that will have
    to manage in separate file for each and every entity class.&lt;br/&gt;
    &lt;code&gt;@Entity&lt;/code&gt;: specifies that the class is an entity. This annotation is applied to the
    entity class.&lt;br/&gt;
    &lt;code&gt;@Table&lt;/code&gt;: specifies the name of the table in database with which entity class is
    mapped.&lt;br/&gt;
    &lt;code&gt;@Id&lt;/code&gt;: This annotation specifies the primary key of the entity.&lt;br/&gt;
    &lt;code&gt;@GeneratedValue&lt;/code&gt;: This annotation specifies the generation strategies for the values
    of primary keys.&lt;br/&gt;
    &lt;code&gt;@Column&lt;/code&gt;: is used to specify the mapped column for a persistent property or
    field.&lt;br/&gt;
    Now let&#39;s see, how Hibernate using these annotations for &lt;code&gt;Message&lt;/code&gt; entity class in
    details:
  &lt;p&gt;The &lt;code&gt;@Entity&lt;/code&gt; annotation tells Hibernate that &lt;code&gt;Message&lt;/code&gt; class is a
    persistent entity that to have its state
    persisted to the &amp;quot;helloworld&amp;quot; database.&lt;br/&gt; The &lt;code&gt;@Table&lt;/code&gt; annotation
    specifies the name of the table and we are mapping it to the &amp;quot;MESSAGE&amp;quot; table.&lt;br/&gt; The
    &lt;code&gt;@Id&lt;/code&gt; annotation indicate that
    &lt;i&gt;id&lt;/i&gt; attribute or field of this class represents a primary key or unique identifier
    of message table and &lt;code&gt;@GeneratedValue&lt;/code&gt; annotation with auto strategy tells hibernate
    that whenever a new message entity is persisted to the &amp;quot;helloworld&amp;quot; database. The
    database generate the unique primary key value for that entity and the hibernate is going to
    set the value of this &lt;i&gt;id&lt;/i&gt; attribute using the newly generated primary key value.
    This is same as what native strategy in the mapping file was doing for us.&lt;br/&gt;
    The &lt;code&gt;@Column&lt;/code&gt; annotation tells Hibernate about the column you want to map this id
    attribute and message attribute to &amp;quot;ID&amp;quot; and &amp;quot;MESSAGE&amp;quot; respectively in the
    &amp;quot;MESSAGE&amp;quot; table.&lt;/p&gt;
  &lt;p&gt;Then one final change we need to make in &lt;code&gt;hibernate.cfg.xml&lt;/code&gt; file is to change the
    mapping &lt;code&gt;resource&lt;/code&gt;
    attribute to a mapping&lt;code&gt;class&lt;/code&gt; attribute and specify the class we declared as entity.
  &lt;/p&gt;
  &lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;HelloWorldClient&lt;/h2&gt;
  &lt;p&gt;This class main method will save the message object into &lt;b&gt;MESSAGE&lt;/b&gt; table of
    &lt;b&gt;helloworld&lt;/b&gt; database.&lt;/p&gt;
  &lt;pre class=&quot;brush: java; title:&#39;HelloWorldClient.java&#39;&quot;&gt;package com.walking.techie;

import com.walking.techie.entity.Message;
import com.walking.techie.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HelloWorldClient {
  public static void main(String[] args) {
    // get session factory of an application
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    // Open a session
    Session session = sessionFactory.openSession();

    // Begin a unit of work and return the associated Transaction object.
    Transaction transaction = session.beginTransaction();

    Message message = new Message(&amp;quot;Hello world with hibernate using JPA annotations&amp;quot;);

    // save the message object into table
    session.save(message);

    // commit the transaction
    transaction.commit();

    // End the session by releasing the JDBC connection and cleaning up.
    session.close();
  }
}&lt;/pre&gt;
  &lt;p&gt;Before running this application you need to create the &lt;b&gt;helloworld&lt;/b&gt; database into MySQL.
    Table will created automatically because we have set the &lt;b&gt;hbm2ddl.auto&lt;/b&gt; to update.&lt;/p&gt;
  &lt;h2 style=&quot;color:blue&quot;&gt;Output&lt;/h2&gt;
  &lt;p&gt;This application will save the message object in &lt;b&gt;MESSAGE&lt;/b&gt; table.&lt;/p&gt;
  &lt;p&gt;output in console&lt;/p&gt;
  &lt;pre class=&quot;brush: java;&quot;&gt;May 30, 2019 3:55:59 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.12.Final}
May 30, 2019 3:55:59 PM org.hibernate.cfg.Environment &amp;lt;clinit&amp;gt;
INFO: HHH000206: hibernate.properties not found
May 30, 2019 3:55:59 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager &amp;lt;clinit&amp;gt;
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
May 30, 2019 3:55:59 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
May 30, 2019 3:55:59 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/helloworld?useSSL=false]
May 30, 2019 3:55:59 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
May 30, 2019 3:55:59 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
May 30, 2019 3:55:59 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections &amp;lt;init&amp;gt;
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
May 30, 2019 3:55:59 PM org.hibernate.dialect.Dialect &amp;lt;init&amp;gt;
INFO: HHH000400: Using dialect: org.hibernate.dialect.MariaDBDialect
May 30, 2019 3:56:00 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@71454b9d] for (non-JTA) DDL execution was not in auto-commit mode; the Connection &#39;local transaction&#39; will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table hibernate_sequence (next_val bigint) engine=InnoDB
Hibernate: insert into hibernate_sequence values ( 1 )
Hibernate: create table MESSAGE (ID bigint not null, MESSAGE varchar(255), primary key (ID)) engine=InnoDB
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into MESSAGE (MESSAGE, ID) values (?, ?)&lt;/pre&gt;
  &lt;p&gt;&lt;strong&gt;&lt;a href=&quot;https://github.com/walkingtechie/hello-world-hibernate-jpa-anno&quot; target=&quot;_blank&quot;&gt;find
    the above working code from git.&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/8794678325045318964/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2019/05/hello-world-application-with-hibernate-using-jpa-annotations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/8794678325045318964'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/8794678325045318964'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2019/05/hello-world-application-with-hibernate-using-jpa-annotations.html' title='Hello World application with Hibernate using JPA annotations'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2rDdKFRtmtfiXBw_bs0B5WymHjBhiRJOtFnFXSl6KPe3byc99sa6s5fyg8ci_d8FYBCrDj4ZFndUIeywwioHoSziaD_x3hu2yVhn0-C13yRUvQdo6Xu8FXCGikFaX3LHEY11-0_sQK0o/s72-c/hello-world-application-using-jpa-annotation.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-6836583209758723904</id><published>2018-12-13T16:41:00.000+05:30</published><updated>2018-12-13T17:40:53.822+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Inserting a node in a singly linked list </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
    &lt;p&gt;In the previous post, we have introduced about &lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/12/introduction-to-linked-list.html&quot; target=&quot;_blank&quot;&gt;Linked
        Lists&lt;/a&gt;. We have also discussed to &lt;a
            href=&quot;http://walkingtechie.blogspot.com/2018/12/implementation-of-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;create
        a singly linked list with 3 nodes&lt;/a&gt;.&lt;/p&gt;
    &lt;p&gt;All program uses the below representation of the Singly linked list.&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;public class SinglyLinkedList&amp;lt;E&amp;gt; {

  // instance variables of the SinglyLinkedList
  private Node&amp;lt;E&amp;gt; head = null; // head node of the list (or null if empty)

  // ---------------- nested Node class ----------------
  private static class Node&amp;lt;E&amp;gt; {
    private E data;
    private Node&amp;lt;E&amp;gt; next;

    public Node(E data) {
      this.data = data;
    }
  }
  // ----------- end of nested Node class -----------
}&lt;/pre&gt;
    &lt;p&gt;In this post, we will discuss to insert a node in the linked list. Below are following ways to insert the node
        in the linked list.&lt;/p&gt;
    &lt;ol&gt;
        &lt;li&gt;Insert a node at the head of a singly linked list.&lt;/li&gt;
        &lt;li&gt;Insert a node at the tail of a singly linked list.&lt;/li&gt;
        &lt;li&gt;Insert a node after given node of a singly linked list.&lt;/li&gt;
    &lt;/ol&gt;
    &lt;h2 style=&quot;color: #880E4F&quot;&gt;Insert a node at the head of a singly linked list.&lt;/h2&gt;
    &lt;p&gt;The new node is always inserted at front of the linked list and newly inserted node become the head of the linked
        list.&lt;/p&gt;
    &lt;p&gt;When using a singly linked list, we can easily insert an element at the head of the list. The main
        idea is that we create a new node, set its data(element) to the new node, set its next
        link to refer to the current head, and set the list’s head to point to the new node.&lt;/p&gt;
    &lt;p&gt;Example: Let&#39;s take an example of singly linked list is &lt;b&gt;20-&gt;30-&gt;40&lt;/b&gt; and we want to insert 10 at the
        head, then the linked list becomes &lt;b&gt;10-&gt;20-&gt;30-&gt;40&lt;/b&gt;.&lt;/p&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHNRZxhgGSM1YDQ-8GWi2OKVJyNc9RK9NJ-CPE00hKZwwvIjGBu_MgTPfEJakWHPY71e6b5AKQbuSC5m3-Sv8RabPch0cv75Of0q-JndF8MAHrrQvB-H6NcCLjVVeWISuxw7S7jqX5DR8/s1600/insert+node+at+front+of+linked+list.jpg&quot;
         alt=&quot;Insert a node at the head of a singly linked list&quot;
         title=&quot;Insert a node at the head of a singly linked list&quot;/&gt;
    &lt;p&gt;Let&#39;s implement a method call it as &lt;code&gt;addFirst&lt;/code&gt; that will insert a node at the head of the linked
        linked.&lt;/p&gt;
    &lt;p&gt;Following are the step to add node at the head of the linked list.&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;/* This method add a node at the head of the linked list or
 * you can say add a node at front of the linked list.*/
private void addFirst(E e) {
  // Create a new node and allocate element or data into it
  Node&amp;lt;E&amp;gt; newNode = new Node&amp;lt;E&amp;gt;(e);

  // refer next of new node to head
  newNode.next = this.head;

  // reassign head reference to the new node
  this.head = newNode;
}&lt;/pre&gt;
    &lt;h2 style=&quot;color: #880E4F&quot;&gt;Insert a node at the tail of a singly linked list.&lt;/h2&gt;
    &lt;p&gt;The new node is always added after the last node of the linked list. Let&#39;s take an example If the given linked
        list is &lt;b&gt;20-&gt;30-&gt;40&lt;/b&gt; and we want to insert 50 at the
        end of the linked list, then the linked list becomes &lt;b&gt;20-&gt;30-&gt;40-&gt;50&lt;/b&gt;.&lt;/p&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicoEOkfaRydyGrcGk0ifxpk1VRya46syPXDY2SsA-ZkyWUafeYx7qdmxxslbR5f4QYo4f-M9PTHOZwajShObqAgqDbK8qSqkihuIqwxPmPNgwHChxbC7tZIahC_EqH38m-97VmWNnTHGc/s1600/insert+node+at+tail+of+linked+list.jpg&quot;
         alt=&quot;Insert a node at the tail of a singly linked list&quot;
         title=&quot;Insert a node at the tail of a singly linked list&quot;/&gt;
    &lt;p&gt;Here we are not maintaining the tail reference of the linked list so we need to traverse the linked to till end
        then change the last node&#39;s next to new node.&lt;/p&gt;
    &lt;p&gt;Following is the Java code to insert node at the end of the linked list.&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;/* This method add a new element at the tail of the linked list or
 * you can say at the end of the linked list.*/
private void addLast(E e) {
  // Create a new node and allocate element or data into it
  Node&amp;lt;E&amp;gt; newNode = new Node&amp;lt;E&amp;gt;(e);

  // New node is going to be the last node, so assign new&#39;s next as null
  // refer next of new node to null
  newNode.next = null;

  // If the linked list is empty then make the new node as head and return.
  if (this.head == null) {
    newNode = this.head;
    return;
  }

  // If linked list not empty then need to reach at the end of linked list
  Node&amp;lt;E&amp;gt; current = this.head;
  while (current.next != null) {
    current = current.next;
  }

  // reassign tail reference to the new node
  current.next = newNode;
}&lt;/pre&gt;
    &lt;h2 style=&quot;color: #880E4F&quot;&gt;Insert a node after given node of a singly linked list.&lt;/h2&gt;
    &lt;p&gt;To insert a node after the given node of the linked list, we will get the reference to the given node as the
        parameter in method call of &lt;code&gt;addAfter&lt;/code&gt;.&lt;/p&gt;
    &lt;p&gt;Example: Let&#39;s take an example of singly linked list is &lt;b&gt;20-&gt;30-&gt;40&lt;/b&gt; and we want to insert a node with data
        35 after the given node 30 in the linked list, then the linked list becomes &lt;b&gt;20-&gt;30-&gt;35-&gt;40&lt;/b&gt;.&lt;/p&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6b1fIBATk483P6x6kC7hHeEKQx6FXkAHXT6EBdi_hCD0y-M6xHO2Ud_BC0PbJyWX3uuJ7JuzNqXJ6RPGQUZGRjI2xgLnDS6B0OYCbAO0NZs4VqRUIuFIlf1-odCBlJEZNIPXNIW-_iBs/s1600/insert-node-after-given-node-of-linked-list.jpg&quot;
         alt=&quot;Insert a node after given node of a singly linked list&quot;
         title=&quot;Insert a node after given node of a singly linked list&quot;/&gt;
    &lt;p&gt;Following is the Java code to insert a node after the given node. This method accept two parameter, one is an
        element and other is reference of the given node after which need to insert a node.&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;/*The addAfter method will add the new element or data after the given node.
 * This method takes two parameters.*/
private void addAfter(E e, Node&amp;lt;E&amp;gt; givenNode) {
  // check if the given node is null and head is null
  if (givenNode == null) {
    System.out.println(&amp;quot;The given node can&#39;t be null.&amp;quot;);
    return;
  }

  // check if the linked list is empty or head is null
  if (this.head == null) {
    System.out.println(&amp;quot;The given linked list is empty.&amp;quot;);
    return;
  }

  // Create a new node and allocate element or data into it
  Node&amp;lt;E&amp;gt; newNode = new Node&amp;lt;E&amp;gt;(e);

  // assign newNode&#39;s next to givenNode&#39;s next
  newNode.next = givenNode.next;

  // assign givenNode&#39;s next as newNode
  givenNode.next = newNode;
}&lt;/pre&gt;
    &lt;hr&gt;
    &lt;p&gt;Following is the complete program of the insertion of node in the linked list. This program has consolidated all
        the discussed method above.&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;public class SinglyLinkedList&amp;lt;E&amp;gt; {

  // instance variables of the SinglyLinkedList
  private Node&amp;lt;E&amp;gt; head = null; // head node of the list (or null if empty)

  // ---------------- nested Node class ----------------
  private static class Node&amp;lt;E&amp;gt; {
    private E data;
    private Node&amp;lt;E&amp;gt; next;

    public Node(E data) {
      this.data = data;
    }
  }
  // ----------- end of nested Node class -----------

  /* instance method of Singly linked list
   * which will print the data of nodes starting from the head.*/
  private void printList() {
    Node&amp;lt;E&amp;gt; current = this.head;

    while (current != null) {
      System.out.print(current.data + &amp;quot;-&amp;gt;&amp;quot;);
      current = current.next;
    }
    System.out.println(&amp;quot;null&amp;quot;);
  }

  // This method adds a node at the head of the linked list or
  // you can say add a node at front of the linked list.
  private void addFirst(E e) {
    // Create a new node and allocate element or data into it
    Node&amp;lt;E&amp;gt; newNode = new Node&amp;lt;E&amp;gt;(e);

    // refer next of new node to head
    newNode.next = this.head;

    // reassign head reference to the new node
    this.head = newNode;
  }

  // This method adds a new element at the tail of the linked list or
  // you can say at the end of the linked list.
  private void addLast(E e) {
    // Create a new node and allocate element or data into it
    Node&amp;lt;E&amp;gt; newNode = new Node&amp;lt;E&amp;gt;(e);

    // New node is going to be the last node, so assign new&#39;s next as null
    // refer next of new node to null
    newNode.next = null;

    // If the linked list is empty then make the new node as head and return.
    if (this.head == null) {
      newNode = this.head;
      return;
    }

    // If linked list not empty then need to reach at the end of linked list
    Node&amp;lt;E&amp;gt; current = this.head;
    while (current.next != null) {
      current = current.next;
    }

    // reassign tail reference to the new node
    current.next = newNode;
  }

  // The addAfter method will add the new element or data after the given node.
  // This method takes two parameters.
  private void addAfter(E e, Node&amp;lt;E&amp;gt; givenNode) {
    // check if the given node is null and head is null
    if (givenNode == null) {
      System.out.println(&amp;quot;The given node can&#39;t be null.&amp;quot;);
      return;
    }

    // check if the linked list is empty or head is null
    if (this.head == null) {
      System.out.println(&amp;quot;The given linked list is empty.&amp;quot;);
      return;
    }

    // Create a new node and allocate element or data into it
    Node&amp;lt;E&amp;gt; newNode = new Node&amp;lt;E&amp;gt;(e);

    // assign newNode&#39;s next to givenNode&#39;s next
    newNode.next = givenNode.next;

    // assign givenNode&#39;s next as newNode
    givenNode.next = newNode;
  }

  // method to create a simple linked list with 3 nodes
  public static void main(String[] args) {
    // Start with the empty list.
    SinglyLinkedList&amp;lt;Integer&amp;gt; singlyLinkedList = new SinglyLinkedList&amp;lt;Integer&amp;gt;();

    // add element to the front of the linked list
    // Linked list become 15-&amp;gt;null
    singlyLinkedList.addFirst(15);

    // add element to the end of the linked list
    // Linked list become 15-&amp;gt;50-&amp;gt;null
    singlyLinkedList.addLast(50);

    // add element 18 after node&#39;s element is 15
    // linked list now become 15-&amp;gt;18-&amp;gt;50-&amp;gt;null
    singlyLinkedList.addAfter(18, singlyLinkedList.head);

    // add element 55 after node&#39;s element is 18
    // linked list now become 15-&amp;gt;18-&amp;gt;55-&amp;gt;50-&amp;gt;null
    singlyLinkedList.addAfter(55, singlyLinkedList.head.next);

    // add element 10 to the front of the linked list
    // linked list now become 10-&amp;gt;15-&amp;gt;18-&amp;gt;55-&amp;gt;50-&amp;gt;null
    singlyLinkedList.addFirst(10);

    // print the elements of the linked list
    singlyLinkedList.printList();
  }
}&lt;/pre&gt;
    &lt;h3&gt;Output:&lt;/h3&gt;
    &lt;div class=&quot;box&quot;&gt;10-&gt;15-&gt;18-&gt;55-&gt;50-&gt;null&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/6836583209758723904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/inserting-node-in-singly-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/6836583209758723904'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/6836583209758723904'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/inserting-node-in-singly-linked-list.html' title='Inserting a node in a singly linked list '/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHNRZxhgGSM1YDQ-8GWi2OKVJyNc9RK9NJ-CPE00hKZwwvIjGBu_MgTPfEJakWHPY71e6b5AKQbuSC5m3-Sv8RabPch0cv75Of0q-JndF8MAHrrQvB-H6NcCLjVVeWISuxw7S7jqX5DR8/s72-c/insert+node+at+front+of+linked+list.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-5917004588236005033</id><published>2018-12-02T18:42:00.000+05:30</published><updated>2018-12-13T17:41:04.914+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Implementation of singly linked list</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
    &lt;p&gt;Singly linked list is a collection of nodes that collectively form a linear sequence. In a singly linked
        list, each node stores data, and as well as a reference to the next node of the list.&lt;/p&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqLAMHG7SZkkvWMuAlgYMR1yBKpug6-gp0WrStyWxwVWp3pbdtGxjurUcwNS-U8PfUWd74L2EL5Gv6KncwI3hfuFCbHIGcKJ3VTqeFrJFZqT7ED9heAwLD04jK9FwuiUozfkJsCfrtYHw/s1600/singly-linked-list.jpg&quot;
         alt=&quot;Singly linked list&quot; title=&quot;Singly linked list&quot;/&gt;
    &lt;h2&gt;Implementation&lt;/h2&gt;
    &lt;p&gt;The linked list instance must keep a reference to the first node of the list, known as the &lt;b&gt;head&lt;/b&gt;. If linked
        list is empty, then value of head is &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;
    &lt;p&gt;Our implementation also takes advantage of Java’s support for nested classes, as we define a private
        &lt;code&gt;Node&lt;/code&gt; class within the scope of the &lt;code&gt;public&lt;/code&gt; &lt;code&gt;SinglyLinkedList&lt;/code&gt; class. Having
        &lt;code&gt;Node&lt;/code&gt; as a nested class provides strong encapsulation, shielding users of our class from the
        underlying details about nodes and links.&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;public class SinglyLinkedList&amp;lt;E&amp;gt; {

  // instance variables of the SinglyLinkedList
  private Node&amp;lt;E&amp;gt; head = null; // head node of the list (or null if empty)

  // ---------------- nested Node class ----------------
  private static class Node&amp;lt;E&amp;gt; {
    private E data;
    private Node&amp;lt;E&amp;gt; next;

    public Node(E data) {
      this.data = data;
    }
  }
  // ----------- end of nested Node class -----------
}&lt;/pre&gt;
    &lt;p&gt;Let&#39;s create first simple singly linked list with 3 nodes.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;SinglyLinkedList.java&#39;&quot;&gt;public class SinglyLinkedList&amp;lt;E&amp;gt; {

  // instance variables of the SinglyLinkedList
  private Node&amp;lt;E&amp;gt; head = null; // head node of the list (or null if empty)

  // ---------------- nested Node class ----------------
  private static class Node&amp;lt;E&amp;gt; {
    private E data;
    private Node&amp;lt;E&amp;gt; next;

    public Node(E data) {
      this.data = data;
    }
  }
  // ----------- end of nested Node class -----------

  /* method to create a simple linked list with 3 nodes*/
  public static void main(String[] args) {
    /* Start with the empty list. */
    SinglyLinkedList&amp;lt;Integer&amp;gt; singlyLinkedList = new SinglyLinkedList&amp;lt;Integer&amp;gt;();

    // creates 3 nodes
    Node&amp;lt;Integer&amp;gt; first = new Node&amp;lt;Integer&amp;gt;(10);
    Node&amp;lt;Integer&amp;gt; second = new Node&amp;lt;Integer&amp;gt;(20);
    Node&amp;lt;Integer&amp;gt; third = new Node&amp;lt;Integer&amp;gt;(30);

    // Three nodes have been allocated  dynamically.
    // and references to these three blocks as first,
    // second and third.
    //
    //       first               second           third
    //         |                   |                 |
    //         |                   |                 |
    //     +----+------+     +----+------+     +----+------+
    //     | 10  |     |     | 20  | null |    |  30 | null |
    //     +----+------+     +----+------+     +----+------+

    // lets assign reference of first to head of singly linked list
    singlyLinkedList.head = first;

    // Singly linked list head is referencing to first node
    //
    // singlyLinkedList.head
    //        and
    //       first               second           third
    //         |                   |                 |
    //         |                   |                 |
    //     +----+------+     +----+------+     +----+------+
    //     | 10  |     |     | 20  | null |    |  30 | null |
    //     +----+------+     +----+------+     +----+------+

    // assign reference of second node to next of first
    first.next = second;

    // Now next of first Node refers to second.  So they
    //   both are linked.
    //
    // singlyLinkedList.head    second           third
    //       |                   |                 |
    //       |                   |                 |
    //   +----+------+     +----+------+     +----+------+
    //   | 10  |  --------&amp;gt;| 20  | null |    |  30 | null |
    //   +----+------+     +----+------+     +----+------+ *

    second.next = third; // Link second node with the third node

    // Now next of second Node refers to third.  So all three
    //     nodes are linked.
    //
    // singlyLinkedList.head        second              third
    //     |                        |           |
    //     |                        |           |
    // +----+------+     +----+------+     +----+------+
    // | 10 |  --------&amp;gt;| 20  |   --------&amp;gt;| 30 | null |
    // +----+------+     +----+------+     +----+------+
  }
}&lt;/pre&gt;
    &lt;h2&gt;Traversal of singly linked list&lt;/h2&gt;
    &lt;p&gt;In the previous example we have created a singly linked list with three nodes. Now we will write a method to
        traverse over a linked list and print the data of the list. Let&#39;s give method name as &lt;code&gt;printList&lt;/code&gt;.
    &lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;SinglyLinkedList.java&#39;; highlight: [19,20,21,22,23,24,25,26,27]&quot;&gt;public class SinglyLinkedList&amp;lt;E&amp;gt; {

  // instance variables of the SinglyLinkedList
  private Node&amp;lt;E&amp;gt; head = null; // head node of the list (or null if empty)

  // ---------------- nested Node class ----------------
  private static class Node&amp;lt;E&amp;gt; {
    private E data;
    private Node&amp;lt;E&amp;gt; next;

    public Node(E data) {
      this.data = data;
    }
  }
  // ----------- end of nested Node class -----------

  // instance method of Singly linked list
  //which will print the data of nodes starting from the head.
  private void printList() {
    Node&amp;lt;E&amp;gt; current = this.head;

    while (current != null) {
      System.out.print(current.data + &amp;quot;---&amp;gt;&amp;quot;);
      current = current.next;
    }
    System.out.println(&amp;quot;null&amp;quot;);
  }

  // method to create a simple linked list with 3 nodes
  public static void main(String[] args) {
    // Start with the empty list.
    SinglyLinkedList&amp;lt;Integer&amp;gt; singlyLinkedList = new SinglyLinkedList&amp;lt;Integer&amp;gt;();

    // creates 3 nodes
    Node&amp;lt;Integer&amp;gt; first = new Node&amp;lt;Integer&amp;gt;(10);
    Node&amp;lt;Integer&amp;gt; second = new Node&amp;lt;Integer&amp;gt;(20);
    Node&amp;lt;Integer&amp;gt; third = new Node&amp;lt;Integer&amp;gt;(30);

    // lets assign reference of first to head of singly linked list
    singlyLinkedList.head = first;

    // assign reference of second node to next of first
    first.next = second;

    // assign reference of third node to next of second
    second.next = third;

    // print the elements of the linked list
    singlyLinkedList.printList();
  }
}&lt;/pre&gt;
    &lt;h3&gt;Output:&lt;/h3&gt;
    &lt;div class=&quot;box&quot;&gt;10---&gt;20---&gt;30---&gt;null&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/5917004588236005033/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/implementation-of-singly-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/5917004588236005033'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/5917004588236005033'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/implementation-of-singly-linked-list.html' title='Implementation of singly linked list'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqLAMHG7SZkkvWMuAlgYMR1yBKpug6-gp0WrStyWxwVWp3pbdtGxjurUcwNS-U8PfUWd74L2EL5Gv6KncwI3hfuFCbHIGcKJ3VTqeFrJFZqT7ED9heAwLD04jK9FwuiUozfkJsCfrtYHw/s72-c/singly-linked-list.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-4032472378516070161</id><published>2018-12-02T16:07:00.000+05:30</published><updated>2018-12-02T16:07:16.982+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Introduction to Linked List in Data Structure</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Introduction&lt;/h1&gt;
    &lt;p&gt;Like array, Linked list is a linear data structure. Unlike array, linked list nodes (elements) are not stored in
        continuous memory location, nodes are connected using reference to the node. Nodes and elements can be used
        interchangeably.&lt;/p&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqLAMHG7SZkkvWMuAlgYMR1yBKpug6-gp0WrStyWxwVWp3pbdtGxjurUcwNS-U8PfUWd74L2EL5Gv6KncwI3hfuFCbHIGcKJ3VTqeFrJFZqT7ED9heAwLD04jK9FwuiUozfkJsCfrtYHw/s1600/singly-linked-list.jpg&quot;
         alt=&quot;Linked List&quot; title=&quot;Linked List&quot;/&gt;
    &lt;p&gt;Each element(node) of a list is comprising of two items - the data and a reference to the next node.&lt;/p&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Why Linked List?&lt;/h1&gt;
    &lt;p&gt;Both Array and Linked list are linear data structure, but have following limitation.&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;The size of an array is fixed. So we must know the upper limit of it in advance. The allocated memory
            is equal to upper limit irrespective of usage.
        &lt;/li&gt;
        &lt;li&gt;Insertion and deletion of an element in an array is expensive. Let&#39;s suppose you need to insert an element
            in
            sorted array then need to create room for new element, for that need to shift the elements of array.
        &lt;/li&gt;
        &lt;p&gt;For example: Lets suppose a sorted array of ids.
        &lt;div class=&quot;box&quot;&gt;ids[] = [100, 110,120,130,140]&lt;/div&gt;
        Now we want to insert an id say &lt;b&gt;115&lt;/b&gt;, then to maintain the sorted order, we have to shift all element
        after 110 to right.&lt;/p&gt;
        &lt;p&gt;Lets we want to delete an id 110 from sorted array, then to maintain the sorted order we have to shift all
            the element to left. Now the sorted array become [100, 120,130,140].&lt;/p&gt;
    &lt;/ul&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Advantage of Linked List&lt;/h1&gt;
    &lt;ul&gt;
        &lt;li&gt;Linked List is dynamic in nature which allocates memory when required. You don&#39;t need to specify the size
            while creating the linked list.
        &lt;/li&gt;
        &lt;li&gt;Ease of insertion and deletion.&lt;/li&gt;
        &lt;li&gt;Stack and queue can be easily implemented using linked list.&lt;/li&gt;
    &lt;/ul&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Disadvantage of Linked List&lt;/h1&gt;
    &lt;ul&gt;
        &lt;li&gt;Memory is wasted as reference need to maintained for next node.&lt;/li&gt;
        &lt;li&gt;Random access of nodes is not possible. We have to access the node sequentially from starting of the first
            node.
        &lt;/li&gt;
        &lt;li&gt;Not cache friendly. Node stored in non-continuous address location.&lt;/li&gt;
    &lt;/ul&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Type of Linked List&lt;/h1&gt;
    &lt;p&gt;There is three type of linked list. They are&lt;/p&gt;
    &lt;ol&gt;
        &lt;li&gt;Singly Linked List&lt;/li&gt;
        &lt;li&gt;Doubly Linked List&lt;/li&gt;
        &lt;li&gt;Circularly Linked List&lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;Let&#39;s know more about them and how they are different from each other.&lt;/p&gt;
    &lt;hr&gt;
    &lt;h2&gt;Singly Linked List&lt;/h2&gt;
    &lt;p&gt;Singly linked list is a collection of nodes that collectively form a linear sequence. In a singly linked
        list, each node stores data, and as well as a reference to the next node of the list.&lt;/p&gt;
    &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqLAMHG7SZkkvWMuAlgYMR1yBKpug6-gp0WrStyWxwVWp3pbdtGxjurUcwNS-U8PfUWd74L2EL5Gv6KncwI3hfuFCbHIGcKJ3VTqeFrJFZqT7ED9heAwLD04jK9FwuiUozfkJsCfrtYHw/s1600/singly-linked-list.jpg&quot;
       imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot;
                            src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqLAMHG7SZkkvWMuAlgYMR1yBKpug6-gp0WrStyWxwVWp3pbdtGxjurUcwNS-U8PfUWd74L2EL5Gv6KncwI3hfuFCbHIGcKJ3VTqeFrJFZqT7ED9heAwLD04jK9FwuiUozfkJsCfrtYHw/s1600/singly-linked-list.jpg&quot;
                            alt=&quot;Singly linked list&quot; title=&quot;Singly linked list&quot;/&gt;&lt;/a&gt;
    &lt;hr&gt;
    &lt;h2&gt;Doubly Linked List&lt;/h2&gt;
    &lt;p&gt;The doubly linked contain data and an explicit reference to the node before it and a reference to the node after
        it.&lt;/p&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSBQFY6bAbhsjlhyphenhyphenAl-9-dHWsCbxm41OaiRbTY5-Z0xuVnaVfUJOq4A0lIh-UC4IYjZvyrV8Lm7D89Jd0mTAeQawXm9W8II_RvP5pCwSD4tFu6ZLjrpea-i_VM3H0mChBmzodic1pL4NQ/s1600/doubly-linked-list.jpg&quot;
         alt=&quot;Doubly linked list&quot; title=&quot;Doubly linked list&quot;/&gt;
    &lt;hr&gt;
    &lt;h2&gt;Circularly Linked List&lt;/h2&gt;
    &lt;p&gt;The circularly linked list is almost same as singly linked list, only difference is that the last node of it
        holds the reference to first node hence forming the circular chain.&lt;/p&gt;
    &lt;img border=&quot;0&quot;
         src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8mcCEuawdY5sLcfu0Dy1jNujDaZ9BkzMDOgbUdWx9-LeZU6kROXYEWgUznyMeWYELd1xzD9dnAHUnvPCS5KLPyD0WMj2cD300ruZDUiXrb5lZcvDjoTg8XxBuvlBIe9oyO8nQpRQsCUo/s1600/curcularly-linked-list.jpg&quot;
         alt=&quot;Circularly linked list&quot; title=&quot;Circularly linked list&quot;/&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/4032472378516070161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/introduction-to-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4032472378516070161'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4032472378516070161'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/introduction-to-linked-list.html' title='Introduction to Linked List in Data Structure'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqLAMHG7SZkkvWMuAlgYMR1yBKpug6-gp0WrStyWxwVWp3pbdtGxjurUcwNS-U8PfUWd74L2EL5Gv6KncwI3hfuFCbHIGcKJ3VTqeFrJFZqT7ED9heAwLD04jK9FwuiUozfkJsCfrtYHw/s72-c/singly-linked-list.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-8384951411487307366</id><published>2018-12-01T19:54:00.000+05:30</published><updated>2020-04-25T23:36:36.814+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Spring Boot"/><title type='text'>Execute schema.sql and data.sql on startup of Spring Boot</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
    &lt;p&gt;An SQL database can be initialized manually and can also be done through code. You can create the table and insert
        the data into tables using JPA.&lt;/p&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Initialise the database using JPA&lt;/h1&gt;
    &lt;p&gt;JPA has feature for DDL(Data definition language: DROP, RENAME, CREATE, ALTER, TRUNCATE) generation, and these
        can be setup to run on startup against database. This is controlled through two external properties:
    &lt;ul&gt;
        &lt;li&gt;
            &lt;code&gt;spring.jpa.generate-ddl&lt;/code&gt; (boolean)
        &lt;/li&gt;
        &lt;dd&gt;By default, its value is &lt;code&gt;false&lt;/code&gt;. Whether to initialize schema on startup and it is vendor
            independent.
        &lt;/dd&gt;
        &lt;li&gt;&lt;code&gt;spring.jpa.hibernate.ddl-auto&lt;/code&gt;&lt;/li&gt;
        &lt;dd&gt;It is a Hibernate feature that control the behavior in more fine grained
            way. It is DDL mode. This is actually a shortcut for the &lt;code&gt;hibernate.hbm2ddl.auto&lt;/code&gt; property.
            Defaults to &lt;code&gt;create-drop&lt;/code&gt; when using an embedded database and no schema manager was detected.
            Otherwise, defaults to &lt;code&gt;none&lt;/code&gt;.
        &lt;/dd&gt;
    &lt;/ul&gt;
    &lt;/p&gt;

    &lt;h1 style=&quot;color: blue&quot;&gt;Initialize a database using Hibernate&lt;/h1&gt;
    &lt;p&gt;You can set &lt;code&gt;spring.jpa.hibernate.ddl-auto&lt;/code&gt; explicitly and standard hibernate properties values are
        enum: &lt;code&gt;none&lt;/code&gt;,&lt;code&gt;validate&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt;, &lt;code&gt;create&lt;/code&gt;,
        &lt;code&gt;create-drop&lt;/code&gt;. Spring boot choose a default value type based on the embedded database. It defaults to
        &lt;code&gt;create-drop&lt;/code&gt; if no schema manager has been detected, and &lt;code&gt;none&lt;/code&gt; is all other cases.
        Available embedded databases in spring boot are &lt;code&gt;hsqldb&lt;/code&gt;, &lt;code&gt;h2&lt;/code&gt;, and &lt;code&gt;derby&lt;/code&gt;. An
        embedded database is detected by looking at &lt;code&gt;Connection&lt;/code&gt; type.&lt;/p&gt;

    &lt;p&gt;You can create the schema from scratch by setting the &lt;code&gt;ddl-auto&lt;/code&gt; properties to &lt;code&gt;create&lt;/code&gt; or
        &lt;code&gt;create-drop&lt;/code&gt; along with a file &lt;code&gt;import.sql&lt;/code&gt; should be in the root of classpath is
        executed on startup. This is the Hibernate feature and has nothing to do with Spring framework. It is advisable
        to do for testing or demos, not in production environment.&lt;/p&gt;

    &lt;h1 style=&quot;color: blue&quot;&gt;Initialize a Database&lt;/h1&gt;
    &lt;p&gt;Spring boot automatically create the schema(DDL scripts) of your &lt;code&gt;DataSource&lt;/code&gt; and initialize it (DML
        scripts). It loads the SQL from the standard root classpath locations: &lt;code&gt;schema.sql&lt;/code&gt; and &lt;code&gt;data.sql&lt;/code&gt;
        respectively. You can create the schema and initialize it based on the platform. Platform value is of
        &lt;code&gt;spring.datasource.platform&lt;/code&gt;. Now you can create files &lt;code&gt;schema-${platform}.sql&lt;/code&gt; and &lt;code&gt;data-${platform}.sql&lt;/code&gt;
        which is processed by Spring Boot. It allows you to choose the database specific scripts if necessary. You can
        choose the vendor name of database(platform)
        like &lt;code&gt;hsqldb&lt;/code&gt;, &lt;code&gt;h2&lt;/code&gt;, &lt;code&gt;oracle&lt;/code&gt;, &lt;code&gt;mysql&lt;/code&gt;, &lt;code&gt;postgresql&lt;/code&gt;, and
        so on.&lt;/p&gt;

    &lt;p&gt;In the JPA based app, You should not use both &lt;code&gt;spring.jpa.hibernate.ddl-auto&lt;/code&gt; and
        &lt;code&gt;schema.sql&lt;/code&gt;. Make sure to disable &lt;code&gt;spring.jpa.hibernate.ddl-auto&lt;/code&gt; if you use &lt;code&gt;schema.sql&lt;/code&gt;.
    &lt;/p&gt;

    &lt;p&gt;Spring Boot automatically creates the schema of embedded &lt;code&gt;DataSource&lt;/code&gt;. You can customize this
        behaviour using &lt;code&gt;spring.datasource.initialization-mode&lt;/code&gt; property. You can always initialize the
        &lt;code&gt;DataSource&lt;/code&gt; regardless of its type setting &lt;code&gt;spring.datasource.initialization-mode=always&lt;/code&gt;.
        The property &lt;code&gt;spring.datasource.initialization-mode&lt;/code&gt; supports three values:
    &lt;ul&gt;
        &lt;li&gt;&lt;code&gt;always&lt;/code&gt;&lt;/li&gt;
        &lt;dd&gt;Always initialize the datasource.&lt;/dd&gt;
        &lt;li&gt;&lt;code&gt;embedded&lt;/code&gt;&lt;/li&gt;
        &lt;dd&gt;Only initialize an embedded datasource.&lt;/dd&gt;
        &lt;li&gt;&lt;code&gt;never&lt;/code&gt;&lt;/li&gt;
        &lt;dd&gt;Do not initialize the datasource.&lt;/dd&gt;
    &lt;/ul&gt;
    &lt;/p&gt;

    &lt;p&gt;By default Spring Boot enables the fail-fast feature of the Spring JDBC initializer. This means application fails
        to start when scripts causes exception. You can tune that behavior by setting
        &lt;code&gt;spring.datasource.continue-on-error&lt;/code&gt;.&lt;/p&gt;


    &lt;h1 style=&quot;color: blue&quot;&gt;Initialize a Spring Batch Database&lt;/h1&gt;
    &lt;p&gt;In Spring Batch, Spring Boot detect your database type and execute the scripts on startup and it happens by
        defaults for the embedded database. You can enable it for any database by setting
        &lt;code&gt;spring.batch.initialize-schema=always&lt;/code&gt;. You can also switch off the initialization explicitly by
        setting &lt;code&gt;spring.batch.initialize-schema=never&lt;/code&gt;.&lt;/p&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Execute schema.sql and data.sql with Spring Boot&lt;/h1&gt;
    &lt;p&gt;Lets initialize a database with spring boot. Create &lt;code&gt;schema.sql&lt;/code&gt; and &lt;code&gt;data.sql&lt;/code&gt; files under
        resources folder. Lets understand with spring boot maven based sample application.&lt;/p&gt;
    &lt;h2 style=&quot;color: blue&quot;&gt;Project dependencies&lt;/h2&gt;
    &lt;pre class=&quot;brush: java; title:&#39;pom.xml&#39;&quot;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;
&amp;lt;project xmlns=&amp;quot;http://maven.apache.org/POM/4.0.0&amp;quot; xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;
         xsi:schemaLocation=&amp;quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&amp;quot;&amp;gt;
    &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;

    &amp;lt;groupId&amp;gt;com.walking.techie&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;sample-data-schema-sql&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;0.0.1&amp;lt;/version&amp;gt;
    &amp;lt;packaging&amp;gt;jar&amp;lt;/packaging&amp;gt;

    &amp;lt;name&amp;gt;sample-data-schema-sql&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;This is a sample spring boot project which demonstrate to load schema.sql and data.sql file
    &amp;lt;/description&amp;gt;

    &amp;lt;parent&amp;gt;
        &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;spring-boot-starter-parent&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;2.0.6.RELEASE&amp;lt;/version&amp;gt;
        &amp;lt;relativePath/&amp;gt; &amp;lt;!-- lookup parent from repository --&amp;gt;
    &amp;lt;/parent&amp;gt;

    &amp;lt;properties&amp;gt;
        &amp;lt;project.build.sourceEncoding&amp;gt;UTF-8&amp;lt;/project.build.sourceEncoding&amp;gt;
        &amp;lt;project.reporting.outputEncoding&amp;gt;UTF-8&amp;lt;/project.reporting.outputEncoding&amp;gt;
        &amp;lt;java.version&amp;gt;1.8&amp;lt;/java.version&amp;gt;
    &amp;lt;/properties&amp;gt;

    &amp;lt;dependencies&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-data-jpa&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;mysql&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;mysql-connector-java&amp;lt;/artifactId&amp;gt;
            &amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.projectlombok&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;lombok&amp;lt;/artifactId&amp;gt;
            &amp;lt;optional&amp;gt;true&amp;lt;/optional&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-test&amp;lt;/artifactId&amp;gt;
            &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;
    &amp;lt;/dependencies&amp;gt;

    &amp;lt;build&amp;gt;
        &amp;lt;plugins&amp;gt;
            &amp;lt;plugin&amp;gt;
                &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
                &amp;lt;artifactId&amp;gt;spring-boot-maven-plugin&amp;lt;/artifactId&amp;gt;
            &amp;lt;/plugin&amp;gt;
        &amp;lt;/plugins&amp;gt;
    &amp;lt;/build&amp;gt;


&amp;lt;/project&amp;gt;&lt;/pre&gt;
    &lt;mark&gt;application.properties&lt;/mark&gt;
    &lt;p&gt;Here, &lt;code&gt;spring.jpa.generate-ddl&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;spring.jpa.hibernate.ddl-auto&lt;/code&gt;
       is &lt;code&gt;none&lt;/code&gt; so that It will not auto generate schema
        scripts. In this example &lt;code&gt;User&lt;/code&gt; is an entity class so corresponding table will not create in
        Database. The property &lt;code&gt;spring.datasource.initialization-mode&lt;/code&gt; control the initialisation of
        datasource with available DDL and DML scripts.&lt;/p&gt;
    &lt;pre class=&quot;brush: java; title:&#39;application.properties&#39;&quot;&gt;spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=true
spring.datasource.username=root
spring.datasource.password=santosh
spring.datasource.initialization-mode=always&lt;/pre&gt;
    &lt;mark&gt;schema.sql&lt;/mark&gt;
    &lt;p&gt;The &lt;code&gt;schema.sql&lt;/code&gt; contains the DDL scripts. We will create person table in Database.&lt;/p&gt;
    &lt;pre class=&quot;brush: java; title:&#39;schema.sql&#39;&quot;&gt;DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
);&lt;/pre&gt;
    &lt;mark&gt;data.sql&lt;/mark&gt;
    &lt;p&gt;The &lt;code&gt;data.sql&lt;/code&gt; contains the DML scripts. We will insert some data into person table.&lt;/p&gt;
    &lt;pre class=&quot;brush: java; title:&#39;data.sql&#39;&quot;&gt;INSERT INTO `person` (name) VALUES (&#39;Santosh&#39;);
INSERT INTO `person` (name) VALUES (&#39;Kumar&#39;);
INSERT INTO `person` (name) VALUES (&#39;Walking&#39;);
INSERT INTO `person` (name) VALUES (&#39;Techie&#39;);&lt;/pre&gt;
    &lt;mark&gt;Java model class&lt;/mark&gt;
    &lt;p&gt;The &lt;code&gt;User&lt;/code&gt; is an entity class.&lt;/p&gt;
    &lt;pre class=&quot;brush: java; title:&#39;User.java&#39;&quot;&gt;package com.walking.techie.entity;

import lombok.Data;

import javax.persistence.*;

@Data
@Table(name = &amp;quot;user&amp;quot;)
@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;
}&lt;/pre&gt;
    &lt;h2 style=&quot;color: blue&quot;&gt;Run Application&lt;/h2&gt;
    &lt;pre class=&quot;brush: java; title:&#39;Application.java&#39;&quot;&gt;package com.walking.techie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}&lt;/pre&gt;
    &lt;p&gt;Once you run this spring boot application. You can see that table &lt;code&gt;person&lt;/code&gt; is created in
        &lt;code&gt;test&lt;/code&gt; Database.&lt;/p&gt;
    &lt;p&gt;You can also create &lt;code&gt;user&lt;/code&gt; table through spring boot auto generate script if you set &lt;code&gt;spring.jpa.generate-ddl&lt;/code&gt;
        to &lt;code&gt;true&lt;/code&gt; or set &lt;code&gt;spring.jpa.hibernate.ddl-auto&lt;/code&gt; to any &lt;code&gt;validate&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt;,
        &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;create-drop&lt;/code&gt;.&lt;/p&gt;
    &lt;p&gt;You can also run scripts based on platform. Lets suppose you want to run for &lt;code&gt;mysql&lt;/code&gt; plateform then
        create files &lt;code&gt;schema-mysql.sql&lt;/code&gt; and &lt;code&gt;data-mysql.sql&lt;/code&gt; under resources folder and set
        property &lt;code&gt;spring.datasource.platform=mysql&lt;/code&gt; in application.properties file.&lt;/p&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/spring-batch-listeners-example.html&quot;
                   target=&quot;_blank&quot;&gt;Spring Batch listener example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/spring-batch-csv-file-to-mysql.html&quot;
                   target=&quot;_blank&quot;&gt;Spring Batch Example in Spring boot - CSV File to MySql Database&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;http://walkingtechie.blogspot.com/2017/03/spring-batch-hello-world-example.html&quot;
                   target=&quot;_blank&quot;&gt;Spring Batch Hello world example using Spring boot&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;
                &lt;a href=&quot;https://walkingtechie.blogspot.com/2018/07/spring-boot-remove-class-field-from-mongodb-document.html&quot;
                   target=&quot;_blank&quot;&gt;Spring Boot - Remove _class field from MongoDB document&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/06/spring-boot-features.html&quot; target=&quot;_blank&quot;&gt;Spring
                Boot Feature&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/07/spring-batch-example-in-spring-boot-csv.html&quot;
                   target=&quot;_blank&quot;&gt;CSV Files with different delimiter to Mongo Database&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/07/using-multiple-datasources-of-mongodb.html&quot;
                   target=&quot;_blank&quot;&gt;Using multiple
                data sources of MongoDB with Spring Boot and Spring Data&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/spring-batch-example-in-spring-boot-csv.html&quot;
                   target=&quot;_blank&quot;&gt;Spring Batch Example in Spring boot - CSV File to Mongo Database&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/method-hiding.html&quot; target=&quot;_blank&quot;&gt;Method hiding in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/java-8-interface-set-2.html&quot; target=&quot;_blank&quot;&gt;Interface
                in Java 8&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/8384951411487307366/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/execute-schema-and-data-sql-on-startup-spring-boot.html#comment-form' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/8384951411487307366'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/8384951411487307366'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/12/execute-schema-and-data-sql-on-startup-spring-boot.html' title='Execute schema.sql and data.sql on startup of Spring Boot'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-4129211607400153502</id><published>2018-11-06T00:54:00.000+05:30</published><updated>2018-11-06T00:54:46.860+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java return statement</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
&lt;img alt=&quot;return statement in Java&quot; title=&quot;return statement in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeyBlj1wJVmJGdlM33jJnRxWW2fvL4VywLVAfFnel8XbifdXWkorNkjwRyaLSWQj8SWXGzvgn7B_qrwVwkNmcZnAjXxsYGbt8v5DoguwBnrdqK54b_tSsybFYqgw4kOvIc0ayHnkNslm4/s1600/java-return-statement.jpg&quot;/&gt;
    &lt;p&gt;The &lt;b&gt;return&lt;/b&gt; statement is used to explicitly return from a method. That is, it causes program control to
        transfer back to the caller of the method. It belongs to &lt;a
                href=&quot;https://walkingtechie.blogspot.com/2018/11/java-control-statements.html&quot; target=&quot;_blank&quot;&gt;jump
            statement.&lt;/a&gt;&lt;/p&gt;
    &lt;p&gt;At any time in a method, the &lt;b&gt;return&lt;/b&gt; statement can be used to control back to caller of the
        method. Thus, the &lt;b&gt;return&lt;/b&gt; statement immediately terminates the method in which it is executed.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;ReturnStatement.java&#39;&quot;&gt;// Demonstrate return statement
class ReturnStatement {

  public static void main(String args[]) {

    boolean t = true;
    System.out.println(&quot;Before the return statement.&quot;);
    if (t) return; // return to caller
    System.out.println(&quot;This won&#39;t execute.&quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;Before the return statement.&lt;/pre&gt;
    &lt;p&gt;In the above program, &lt;b&gt;return&lt;/b&gt; statement used inside the &lt;b&gt;main()&lt;/b&gt; method. Java run-time system calls
        the &lt;b&gt;main&lt;/b&gt; method, so &lt;b&gt;return&lt;/b&gt; causes execution to return to the Java run-time system.&lt;/p&gt;
    &lt;p&gt;As you can see, the final println( ) statement is not executed. As soon as return is executed, control passes
        back to the caller.&lt;/p&gt;
    &lt;p&gt;&lt;em&gt;Important point:&lt;/em&gt; &lt;b&gt;if(t)&lt;/b&gt; statement is necessary. Without it, the
        Java compiler would flag an &quot;unreachable code&quot; error because the compiler would know that
        the last println() statement would never be executed. To prevent this error, the &lt;a
                href=&quot;https://walkingtechie.blogspot.com/2018/11/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;if
            statement&lt;/a&gt; is used here to trick the compiler for the sake of this demonstration.&lt;/p&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-break-statement.html&quot;
                   target=&quot;_blank&quot;&gt;Break statement in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/factorial-of-large-number.html&quot; target=&quot;_blank&quot;&gt;Factorial
                of arbitrary large number&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-comments.html&quot; target=&quot;_blank&quot;&gt;Comments in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Operators in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/scope-of-variables-in-java.html&quot; target=&quot;_blank&quot;&gt;Scope
                of variables in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/arithmetic-operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Arithmetic
                operators in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;Java if
                else statement&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&quot; target=&quot;_blank&quot;&gt;Java while
                loop&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-do-while-loop.html&quot; target=&quot;_blank&quot;&gt;Do while
                loop in Java&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/4129211607400153502/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-return-statement.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4129211607400153502'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4129211607400153502'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-return-statement.html' title='Java return statement'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeyBlj1wJVmJGdlM33jJnRxWW2fvL4VywLVAfFnel8XbifdXWkorNkjwRyaLSWQj8SWXGzvgn7B_qrwVwkNmcZnAjXxsYGbt8v5DoguwBnrdqK54b_tSsybFYqgw4kOvIc0ayHnkNslm4/s72-c/java-return-statement.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-5096311596427753984</id><published>2018-11-06T00:53:00.000+05:30</published><updated>2018-11-06T00:53:09.785+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Array in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
    &lt;img alt=&quot;Array in Java&quot; title=&quot;Array in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDuh1Y0vqrn_aTBqiTGp7mnbYq2EOfclRvcsjyHnvRetlxkThrxUSXC4KImHMug0KuOiTqqJN-BQBYtmp3OHbdQqBuYCP6DVddTibM6ZI9IVKu0NUhC63HOHhn4TCmz5OTo38mj3OQId8/s1600/array-in-java.jpg&quot;/&gt;
    &lt;p&gt;An array is a group of like-typed variables that are referred to by a common name. Arrays of
        any type can be created and may have one or more dimensions. A specific element in an
        array is accessed by its index.&lt;/p&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;One-Dimensional Arrays&lt;/h1&gt;
    &lt;p&gt;The general form of a one-dimensional array declaration is&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;type var-name[ ];
or
type[] var-name;
    &lt;/pre&gt;
    &lt;p&gt;An array declaration has two components: the &lt;i&gt;type&lt;/i&gt; and the &lt;i&gt;name&lt;/i&gt;. &lt;i&gt;type&lt;/i&gt; declares the element
        type of the array. The
        element type determines the data type of each element that comprises the array. Like array of int type, we can
        also create an array of other primitive data types like char, float, double etc.. or user defined data
        type(objects of a class).Thus, the element type for the array determines what type of data the array will
        hold.&lt;/p&gt;
    &lt;pre class=&quot;brush: java;&quot;&gt;// both declaration are valid
int ia[];
int[] ia2;
    &lt;/pre&gt;
    &lt;p&gt;Although this declaration establishes the fact that &lt;b&gt;ia&lt;/b&gt; is an array variable, no
        array actually exists. To link &lt;b&gt;ia&lt;/b&gt; with an actual, physical array of integers, you must
        allocate one using &lt;code&gt;new&lt;/code&gt; and assign it to &lt;b&gt;ia&lt;/b&gt;. &lt;code&gt;new&lt;/code&gt; is a special operator that
        allocates memory.&lt;/p&gt;
    &lt;h2 style=&quot;color: crimson;&quot;&gt;Initiating the one dimensional Array in Java&lt;/h2&gt;
    &lt;p&gt;When an array is declared, only reference of an array is created. To actually create the physical form of an
        array, need to allocate memory to array. The general form of new as it applies to one-dimensional arrays appears
        as follows:&lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;array-var = new type [size];&lt;/div&gt;
    &lt;p&gt;Here, &lt;i&gt;type&lt;/i&gt; specifies the type of data being allocated, &lt;i&gt;size&lt;/i&gt; specifies the number of elements in
        array, and &lt;i&gt;array-var&lt;/i&gt; is the variable name of array. The &lt;code&gt;new&lt;/code&gt; is used to allocate the memory
        to an array.&lt;/p&gt;
    &lt;p&gt;The elements of an array allocated by &lt;code&gt;new&lt;/code&gt; will automatically initialized to zero (for numeric type),
        false (for boolean), and &lt;code&gt;null&lt;/code&gt; (for reference types).&lt;/p&gt;
    &lt;p&gt;Example:&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;int intArray[] = new int[10];&lt;/pre&gt;
    OR
    &lt;pre class=&quot;brush: java&quot;&gt;int intArray[]; // declare an array
intArray = new int[10]; // initialize an array&lt;/pre&gt;
    &lt;p&gt;After the above statement executes, &lt;i&gt;intArray&lt;/i&gt; will refer to an array of 10 elements. Further, all element
        in the array will be initialized to zero.&lt;/p&gt;
    &lt;p&gt;Creating an array in java is two step process. First, you must declare a variable of desired data type.
        Second, you must allocate the memory that will hold the array, using new, and assign to an array variable.&lt;/p&gt;
    &lt;p&gt;In Java, all arrays are dynamically allocated.&lt;/p&gt;
    &lt;p&gt;The element of the array can access by using the index inside the square brackets. All array indexes start at
        zero.&lt;/p&gt;
    &lt;pre class=&quot;brush: java; title:&#39;ArrayDemo.java&quot;&gt;public class ArrayDemo {
  public static void main(String[] args) {
    int month_days[];
    month_days = new int[12];
    month_days[0] = 31;
    month_days[1] = 28;
    month_days[2] = 31;
    month_days[3] = 30;
    month_days[4] = 31;
    month_days[5] = 30;
    month_days[6] = 31;
    month_days[7] = 31;
    month_days[8] = 30;
    month_days[9] = 31;
    month_days[10] = 30;
    month_days[11] = 31;
    System.out.println(&amp;quot;April has &amp;quot; + month_days[3] + &amp;quot; days.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;Output of the program:&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;April has 30 days.&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson;&quot;&gt;Array literals&lt;/h2&gt;
    &lt;p&gt;Array can be initialized when they are declared. An &lt;i&gt;array initializer&lt;/i&gt; is a list of comma-separated
        expressions surrounded by curly braces. The array will automatically be created large enough to hold the number
        of elements you specify in the array initializer.&lt;/p&gt;
    &lt;p&gt;There is no need to use new.&lt;/p&gt;
    &lt;pre class=&quot;brush: java; title:&#39;ArrayDemo.java&#39;&quot;&gt;// An improved version of the previous program.
class ArrayDemo {
  public static void main(String args[]) {
    int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    System.out.println(&amp;quot;April has &amp;quot; + month_days[3] + &amp;quot; days.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;Java strictly checked the range of array when you try to store or reference value and access the array
        element.&lt;/p&gt;
    &lt;p&gt;For example, the run-time system will check the value of each index into month_days to make sure that it is
        between 0 and 11 inclusive.&lt;/p&gt;
    &lt;p&gt;If you try to access elements outside the range of the array (negative numbers or numbers
        greater than the length of the array), you will get a run-time error(ArrayIndexOutOfBoundsException).&lt;/p&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Accessing elements of array using for loop&lt;/h2&gt;
    &lt;p&gt;Lets create an example that demonstrate average of elements of array.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Refer: &lt;/b&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-for-loop.html&quot; target=&quot;_blank&quot;&gt;For loop in
        Java in detail.&lt;/a&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush: java; title:&#39;Average.java&quot;&gt;public class Average {
  public static void main(String args[]) {
    double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
    double result = 0;
    int i;
    int length = nums.length;

    // for loop
    for (i = 0; i &amp;lt; length; i++) result = result + nums[i];
    System.out.println(&amp;quot;Average is &amp;quot; + result / length);

    result = 0;
    // foreach
    for (double num : nums) {
      result = result + num;
    }
    System.out.println(&amp;quot;Average is &amp;quot; + result / length);
  }
}&lt;/pre&gt;
    &lt;p&gt;Arrays are object in Java so that we can find the length of array using the &lt;b class=&quot;param&quot;&gt;length&lt;/b&gt; member.
    &lt;/p&gt;
    &lt;p&gt;Output of the above program:&lt;/p&gt;
    &lt;pre class=&quot;brush: java;&quot;&gt;Average is 12.299999999999999
Average is 12.299999999999999&lt;/pre&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Multidimensional Arrays&lt;/h1&gt;
    &lt;p&gt;In Java, multidimensional arrays are actually &lt;b&gt;arrays of arrays&lt;/b&gt;. To declare a multidimensional array
        variable, specify each additional
        index using another set of square brackets([]).&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;int twoD[][]= new int[3][5]; // create 3 by 5 array(2D array)
int threeD[][][]=new int[3][4][5]; // create 3 by 4 by 5 array(3D array)
&lt;/pre&gt;
    &lt;pre class=&quot;brush: java; title:&#39;TwoDArray.java&#39;&quot;&gt;public class TwoDArray {
  public static void main(String[] args) {
    int twoD[][] = new int[3][3];
    int i, j, k = 0;

    for (i = 0; i &amp;lt; 3; i++) {
      for (j = 0; j &amp;lt; 3; j++) {
        twoD[i][j] = k++;
      }
    }

    // print elements of 2D array
    for (i = 0; i &amp;lt; 3; i++) {
      for (j = 0; j &amp;lt; 3; j++) {
        System.out.print(twoD[i][j] + &amp;quot; &amp;quot;);
      }
      System.out.println();
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output of the above program&lt;/p&gt;
    &lt;pre class=&quot;brush: java;&quot;&gt;0 1 2
3 4 5
6 7 8 &lt;/pre&gt;
    &lt;!-- Need to write more about multi-dimension array--&gt;
    &lt;p&gt;When you allocate memory for multi-dimensional array, you can specify the memory for the first (leftmost)
        dimension. You can allocate the remaining dimensions separately.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;int twoD[][] = new int[3][];
twoD[0] = new int[4];
twoD[1] = new int[4];
twoD[2] = new int[4];&lt;/pre&gt;
    &lt;p&gt;There is no advantage of individually allocating the second dimension in this situation.&lt;/p&gt;
    &lt;p&gt;Let&#39;s see an example with working program.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;UnEqualTwoDArray.java&#39;&quot;&gt;// demonstrate 2D array where manually allocate different size of second dimensions
public class UnEqualTwoDArray {

  public static void main(String[] args) {

    int twoD[][] = new int[3][];
    twoD[0] = new int[1];
    twoD[1] = new int[2];
    twoD[2] = new int[3];

    for (int i = 0; i &amp;lt; twoD.length; i++) {
      for (int j = 0; j &amp;lt; twoD[i].length; j++) {
        twoD[i][j] = i * j;
      }
    }

    // print elements of twoD array
    for (int i = 0; i &amp;lt; twoD.length; i++) {
      for (int j = 0; j &amp;lt; twoD[i].length; j++) {
        System.out.print(twoD[i][j] + &amp;quot; &amp;quot;);
      }
      System.out.println();
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;0
0 1
0 2 4 &lt;/pre&gt;
    &lt;p&gt;Let&#39;s see one more example of multidimensional array. The following program creates 3 by 4 by 5, three
        dimensional array.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;UnEqualTwoDArray.java&#39;&quot;&gt;// Demonstrate a three-dimensional array.
class ThreeDMatrix {
  public static void main(String args[]) {
    // 3D array of 3 by 4 by 5
    int threeD[][][] = new int[3][4][5];

    int i, j, k;

    for (i = 0; i &amp;lt; 3; i++) {
      for (j = 0; j &amp;lt; 4; j++) {
        for (k = 0; k &amp;lt; 5; k++) {
          threeD[i][j][k] = i * j * k;
        }
      }
    }

    // print 3D array
    for (i = 0; i &amp;lt; 3; i++) {
      for (j = 0; j &amp;lt; 4; j++) {
        for (k = 0; k &amp;lt; 5; k++) {
          System.out.print(threeD[i][j][k] + &amp;quot; &amp;quot;);
        }
        System.out.println();
      }
      System.out.println();
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12

0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24&lt;/pre&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Alternative Array Declaration Syntax&lt;/h1&gt;
    &lt;p&gt;Declare one dimension array.&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;type[ ] var-name;&lt;/pre&gt;
    &lt;p&gt;OR&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;type var-name[ ];&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;p&gt;Following two declarations are equivalent:&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;int a1[] = new int[5];
int[] a2 = new int[5];&lt;/pre&gt;

    &lt;p&gt;Declare multidimensional array.&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;type[ ][ ] var-name;&lt;/pre&gt;
    &lt;p&gt;OR&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;type var-name[ ][ ];&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;p&gt;Following two declarations are equivalent:&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;int a1[][] = new int[3][5];
int[][] a2 = new int[3][5];&lt;/pre&gt;
    &lt;p&gt;You can declare multiple arrays at the same time using comma separated.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;p&gt;Following two declarations are equivalent:&lt;/p&gt;
    &lt;pre class=&quot;brush: java&quot;&gt;int a1[], a2[], a3[]; // create three arrays of type int
int[] b1, b2, b3; // create three arrays of type int&lt;/pre&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-break-statement.html&quot;
                   target=&quot;_blank&quot;&gt;Break statement in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-continue-statement.html&quot;
                   target=&quot;_blank&quot;&gt;Continue statement in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-for-loop.html&quot; target=&quot;_blank&quot;&gt;For loop in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-comments.html&quot; target=&quot;_blank&quot;&gt;Comments in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Operators in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/scope-of-variables-in-java.html&quot; target=&quot;_blank&quot;&gt;Scope
                of variables in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/arithmetic-operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Arithmetic
                operators in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;Java if
                else statement&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&quot; target=&quot;_blank&quot;&gt;Java while
                loop&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/5096311596427753984/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/array-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/5096311596427753984'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/5096311596427753984'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/array-in-java.html' title='Array in Java'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDuh1Y0vqrn_aTBqiTGp7mnbYq2EOfclRvcsjyHnvRetlxkThrxUSXC4KImHMug0KuOiTqqJN-BQBYtmp3OHbdQqBuYCP6DVddTibM6ZI9IVKu0NUhC63HOHhn4TCmz5OTo38mj3OQId8/s72-c/array-in-java.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-4213629318532061206</id><published>2018-11-04T13:55:00.000+05:30</published><updated>2018-11-04T13:55:19.409+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java continue statement</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
    &lt;img alt=&quot;Continue statement in Java&quot; title=&quot;Continue statement in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6TBbtmomXa9e86UiW_iB1q1Kq69FhREoQ6B0Um-C5ho8UHLUf7kkHuAhWy7Eddg0Glu20tu6c63DNupbFuGNCkLBUWeuFw-QqwC6RZzH8eANlHPLJD2EMTDjIFettRAkP8w9trQxxxyw/s1600/java-continue-statement.jpg&quot;/&gt;
    &lt;p&gt;The &lt;b&gt;continue&lt;/b&gt; statement is used to control the flow of execution of loops. In &lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&quot; target=&quot;_blank&quot;&gt;while&lt;/a&gt; and &lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/11/java-do-while-loop.html&quot; target=&quot;_blank&quot;&gt;do-while&lt;/a&gt;
        loops, a &lt;b&gt;continue&lt;/b&gt; statement causes control to be transferred directly to the conditional expression
        that controls the loop. In a &lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-for-loop.html&quot;
                                        target=&quot;_blank&quot;&gt;for loop&lt;/a&gt;, control goes first to the iteration portion of the
        &lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-for-loop.html&quot;
           target=&quot;_blank&quot;&gt;for statement&lt;/a&gt; and then to the conditional expression. For all three loops, any
        intermediate code is bypassed.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;continueForLoop.java&#39;&quot;&gt;// continue statement with for loop
public class continueForLoop {
  public static void main(String[] args) {

    for (int i = 0; i &amp;lt; 10; i++) {
      System.out.print(i + &amp;quot; &amp;quot;);

      if (i % 2 == 0) continue;

      System.out.println();
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;0 1
2 3
4 5
6 7
8 9
&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;continue statement with label for loop&lt;/h2&gt;
    &lt;p&gt;As you can use label &lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-break-statement.html&quot;
                               target=&quot;_blank&quot;&gt;break statement&lt;/a&gt;, &lt;b&gt;continue&lt;/b&gt; can also use the label to describe
        which enclosing loop to continue.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;p&gt;Print triangular multiplication table for 0 to 9 using the &lt;b&gt;continue&lt;/b&gt; statement.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;TriangularMultiplication.java&#39;&quot;&gt;/ Using continue with a label.
class TriangularMultiplication {
  public static void main(String args[]) {

    outer:
    for (int i = 0; i &amp;lt; 10; i++) {

      for (int j = 0; j &amp;lt; 10; j++) {

        if (j &amp;gt; i) {
          System.out.println();
          continue outer;
        }

        System.out.print(i * j + &amp;quot; &amp;quot;);
      } // inner loop
    } // outer loop
    System.out.println(&amp;quot;\nOutside the outer label for loop&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
Outside the outer label for loop&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java continue statement with do-while loop&lt;/h2&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;ContinueDoWhileLoop.java&#39;&quot;&gt;// continue statement with do-while loop
public class ContinueDoWhileLoop {
  public static void main(String[] args) {
    int i = 0;
    do {
      if (i == 5) continue;

      System.out.println(&amp;quot;Value of i= &amp;quot; + i);

    } while (++i &amp;lt; 10);

    System.out.println(&amp;quot;Outside the do-while loop.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Value of i= 0
Value of i= 1
Value of i= 2
Value of i= 3
Value of i= 4
Value of i= 6
Value of i= 7
Value of i= 8
Value of i= 9
Outside the do-while loop.&lt;/pre&gt;
    &lt;p&gt;As you can see in the above output. Value of i= 5 is not printed.&lt;/p&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java continue statement with while loop&lt;/h2&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakWhileLoop.java&#39;&quot;&gt;/ continue statement with while loop
public class ContinueWhileLoop {
  public static void main(String[] args) {
    int i = 0;
    while (i &amp;lt; 5) {
      i++; // value of i is increased by 1
      if (i == 2) continue;
      System.out.println(&amp;quot;Value of i= &amp;quot; + i);
    }

    System.out.println(&amp;quot;Outside the while loop.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Value of i= 1
Value of i= 3
Value of i= 4
Value of i= 5
Outside the while loop.&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java break statement inside nested loop&lt;/h2&gt;
    &lt;p&gt;Java &lt;b&gt;continue&lt;/b&gt; statement will continue the inner loop only when if you used the continue statement inside
        it.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;ContinueNestedtLoop.java&#39;&quot;&gt;// demonstrate continue statement with nested loop
public class ContinueNestedtLoop {

  public static void main(String[] args) {
    // Outer loop
    for (int i = 0; i &amp;lt; 3; i++) {
      System.out.println(&amp;quot;Outer loop: i= &amp;quot; + i);

      // Inner loop
      for (int j = 1; j &amp;lt;= 3; j++) {
        if (j == 2) continue; // continue the inner loop

        System.out.println(&amp;quot; Inner loop: j= &amp;quot; + j);
      }
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Outer loop: i= 0
 Inner loop: j= 1
 Inner loop: j= 3
Outer loop: i= 1
 Inner loop: j= 1
 Inner loop: j= 3
Outer loop: i= 2
 Inner loop: j= 1
 Inner loop: j= 3&lt;/pre&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-break-statement.html&quot;
                   target=&quot;_blank&quot;&gt;Break statement in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/factorial-of-large-number.html&quot; target=&quot;_blank&quot;&gt;Factorial
                of arbitrary large number&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-comments.html&quot; target=&quot;_blank&quot;&gt;Comments in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Operators in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/scope-of-variables-in-java.html&quot; target=&quot;_blank&quot;&gt;Scope
                of variables in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/arithmetic-operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Arithmetic
                operators in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;Java if
                else statement&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&quot; target=&quot;_blank&quot;&gt;Java while
                loop&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-do-while-loop.html&quot; target=&quot;_blank&quot;&gt;Do while
                loop in Java&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/4213629318532061206/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-continue-statement.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4213629318532061206'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4213629318532061206'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-continue-statement.html' title='Java continue statement'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6TBbtmomXa9e86UiW_iB1q1Kq69FhREoQ6B0Um-C5ho8UHLUf7kkHuAhWy7Eddg0Glu20tu6c63DNupbFuGNCkLBUWeuFw-QqwC6RZzH8eANlHPLJD2EMTDjIFettRAkP8w9trQxxxyw/s72-c/java-continue-statement.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-4649373657770502963</id><published>2018-11-04T12:30:00.000+05:30</published><updated>2018-11-04T12:30:20.783+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java break statement</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
&lt;img alt=&quot;Break statement in Java&quot; title=&quot;Break statement in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGMPIPGSAp50uq-M5ArOvogQkQIVV7OKsi1Tb3B_ixDHor30huCtaQRw81wllOvjiT9tX8zdIbtpZ7qPppMAb-bdkwRmfoGWMrHsPmc2DNM0ldUXluOkuMDoIgMo-pjD_ixw4gv0HaqHA/s1600/java-break-statement.jpg&quot;/&gt;
    &lt;p&gt;Java break statement has three uses:&lt;/p&gt;
    &lt;ol&gt;
        &lt;li&gt;It can use to exit a loop.&lt;/li&gt;
        &lt;li&gt;it can be used as a &quot;civilized&quot; form of goto i.e label&lt;/li&gt;
        &lt;li&gt;It terminates a statement sequence in &lt;a
                href=&quot;https://walkingtechie.blogspot.com/2018/11/switch-statement-in-java.html&quot; target=&quot;_blank&quot;&gt;switch
            statement.&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Using break to exit a loop&lt;/h1&gt;
    &lt;p&gt;By using &lt;b&gt;break&lt;/b&gt;, you can force immediate termination of a loop, bypassing the conditional
        expression and any remaining code in the body of the loop. When a &lt;b&gt;break&lt;/b&gt; statement is
        encountered inside a loop, the loop is terminated and program control resumes at the next
        statement following the loop.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakForLoop.java&#39;&quot;&gt;/ Using break to exit from a loop
public class BreakForLoop {
  public static void main(String[] args) {
    for (int i = 0; i &amp;lt; 10; i++) {
      System.out.println(&amp;quot;Value of i= &amp;quot; + i);
      if (i == 5) {
        break;
      }
    }
    System.out.println(&amp;quot;Outside the for loop.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Value of i= 0
Value of i= 1
Value of i= 2
Value of i= 3
Value of i= 4
Value of i= 5
Outside the for loop.&lt;/pre&gt;
    &lt;p&gt;As you can see the output, although the &lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-for-loop.html&quot;
                                                  target=&quot;_blank&quot;&gt;for loop&lt;/a&gt; is designed to run from 0 to 9, the break
        statement caused it to terminate early, when i equals to 5.&lt;/p&gt;
    &lt;p&gt;Java break statement can be used with any Java&#39;s loops, including intentionally infinite loops.&lt;/p&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java break statement with do-while loop&lt;/h2&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakDoWhileLoop.java&#39;&quot;&gt;// break statement with do-while loop
public class BreakDoWhileLoop {
  public static void main(String[] args) {
    int i = 0;
    do {
      System.out.println(&amp;quot;Value of i= &amp;quot; + i);

      if (i == 5) break;

    } while (++i &amp;lt; 10);

    System.out.println(&amp;quot;Outside the do-while loop.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Value of i= 0
Value of i= 1
Value of i= 2
Value of i= 3
Value of i= 4
Value of i= 5
Outside the do-while loop.&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java break statement with while loop&lt;/h2&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakWhileLoop.java&#39;&quot;&gt;// break statement with while loop
public class BreakWhileLoop {
  public static void main(String[] args) {
    int i = 0;
    while (i &amp;lt; 10) {
      System.out.println(&amp;quot;Value of i= &amp;quot; + i);

      if (i == 5) break;
      i++;
    }

    System.out.println(&amp;quot;Outside the while loop.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Value of i= 0
Value of i= 1
Value of i= 2
Value of i= 3
Value of i= 4
Value of i= 5
Outside the while loop.&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java break statement inside nested loop&lt;/h2&gt;
    &lt;p&gt;Java break statement will only break out from the innermost loop, when it is used inside a set of nested
        loops.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakInnerMostLoop.java&#39;&quot;&gt;// demonstrate break statement with nested loop
// break statement break out from the innermost loop.
public class BreakInnerMostLoop {

  public static void main(String[] args) {
    // Outer loop
    for (int i = 0; i &amp;lt; 10; i++) {
      System.out.println(&amp;quot;Outer loop: i= &amp;quot; + i);

      // Inner loop
      for (int j = 0; j &amp;lt; 10; j++) {
        System.out.println(&amp;quot; Inner loop: j= &amp;quot; + j);
        if (j == 2) break; // break out from the inner loop
      }
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Outer loop: i= 0
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 1
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 2
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 3
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 4
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 5
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 6
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 7
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 8
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2
Outer loop: i= 9
 Inner loop: j= 0
 Inner loop: j= 1
 Inner loop: j= 2&lt;/pre&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Using break as a form of goto (label)&lt;/h1&gt;
    &lt;p&gt;Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner.
        This usually makes goto-ridden code hard to understand and hard to maintain. It also prohibits certain compiler
        optimizations.&lt;/p&gt;
    &lt;p&gt;The goto can be useful when you are exiting from a deeply nested set of loops. Java provides &lt;b&gt;labeled&lt;/b&gt; break
        statement to achieve it. Using this form of the &lt;b&gt;break&lt;/b&gt; statement, you can break out of one or more blocks
        of code. These blocks need not be part of a loop or a switch. They can be any block.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/p&gt;
    &lt;div class=&quot;box&quot;&gt;break label;&lt;/div&gt;
    &lt;p&gt;&lt;i&gt;abel&lt;/i&gt; is the name of a label that identifies a block of code.&lt;/p&gt;
    &lt;p&gt;The labeled block must enclose the &lt;b&gt;break&lt;/b&gt; statement, but it does not need to be the immediately enclosing
        block.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;p&gt;Using labeled &lt;b&gt;break&lt;/b&gt; statement with if block.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakBlocksOfCode.java&#39;&quot;&gt;// Using break as a civilized form of goto.
class BreakBlocksOfCode {
  public static void main(String args[]) {
    boolean t = true;

    first:
    {
      second:
      {
        third:
        {
          System.out.println(&amp;quot;Before the break.&amp;quot;);
          if (t) break second; // break out of second block
          System.out.println(&amp;quot;This won&#39;t execute&amp;quot;);
        } // end of third label
        System.out.println(&amp;quot;This won&#39;t execute&amp;quot;);
      } // end of second label
      System.out.println(&amp;quot;This is after second block.&amp;quot;);
    } // end of first label
    System.out.println(&amp;quot;This is after first block.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Before the break.
This is after second block.
This is after first block.&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java break statement with labeled for loop&lt;/h2&gt;
    &lt;p&gt;Mostly labeled &lt;b&gt;break&lt;/b&gt; statement is used to break out from nested &lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/10/java-for-loop.html&quot;
            target=&quot;_blank&quot;&gt;for loop.&lt;/a&gt;&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakNestedLoop.java&#39;&quot;&gt;// Using break to exit from nested loops
class BreakNestedLoop {
  public static void main(String args[]) {

    outer:
    for (int i = 0; i &amp;lt; 3; i++) {
      System.out.print(&amp;quot;Pass &amp;quot; + i + &amp;quot;: &amp;quot;);

      for (int j = 0; j &amp;lt; 10; j++) {
        if (j == 5) break outer; // exit both loops
        System.out.print(j + &amp;quot; &amp;quot;);
      } // end of inner loop

      System.out.println(&amp;quot;This will not print&amp;quot;);
    } // end of outer label loop

    System.out.println(&amp;quot;Loops complete.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Pass 0: 0 1 2 3 4 Loops complete.&lt;/pre&gt;
    &lt;p&gt;Keep in mind that you cannot break to any label which is not defined for an enclosing block.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;BreakErr.java&#39;&quot;&gt;// This program contains an error.
class BreakErr {
  public static void main(String args[]) {
    one:
    for (int i = 0; i &amp;lt; 3; i++) {
      System.out.print(&amp;quot;Pass &amp;quot; + i + &amp;quot;: &amp;quot;);
    }

    for (int j = 0; j &amp;lt; 100; j++) {
      if (j == 10) break one; // compile time error
      System.out.print(j + &amp;quot; &amp;quot;);
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Compile give error like java: undefined label: one&lt;/p&gt;
    &lt;p&gt;Since the loop labeled &lt;b&gt;one&lt;/b&gt; does not enclose the &lt;b&gt;break&lt;/b&gt; statement, it is not possible to
        transfer control out of that block&lt;/p&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Java break Statement with Switch&lt;/h1&gt;
    &lt;p&gt;The break that terminates a &lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/11/switch-statement-in-java.html&quot; target=&quot;_blank&quot;&gt;switch
        statement&lt;/a&gt; affects only that switch statement and not any enclosing loops.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;ForLoopWithSwitch.java&#39;&quot;&gt;public class ForLoopWithSwitch {
  public static void main(String[] args) {
    for (int i = 0; i &amp;lt; 5; i++) {
      System.out.println(&amp;quot;Value of i= &amp;quot; + i);

      switch (i) {
        case 0:
          System.out.println(&amp;quot;Zero&amp;quot;);
          break;
        case 1:
          System.out.println(&amp;quot;One&amp;quot;);
          break;
        case 2:
          System.out.println(&amp;quot;Two&amp;quot;);
          break;
        case 3:
          System.out.println(&amp;quot;Three&amp;quot;);
          break;
        case 4:
          System.out.println(&amp;quot;Four&amp;quot;);
          break;
        default:
          System.out.println(&amp;quot;Values is not between 1 to 4.&amp;quot;);
      }
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Value of i= 0
Zero
Value of i= 1
One
Value of i= 2
Two
Value of i= 3
Three
Value of i= 4
Four&lt;/pre&gt;
    &lt;p&gt;To understand the example of the break with the switch statement, please visit here: &lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/11/switch-statement-in-java.html&quot; target=&quot;_blank&quot;&gt;Java Switch
        Statement&lt;/a&gt;.&lt;/p&gt;

    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/what-are-different-ways-to-load-class.html&quot;
                   target=&quot;_blank&quot;&gt;How to load class in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/factorial-of-large-number.html&quot; target=&quot;_blank&quot;&gt;Factorial
                of arbitrary large number&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-comments.html&quot; target=&quot;_blank&quot;&gt;Comments in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Operators in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/scope-of-variables-in-java.html&quot; target=&quot;_blank&quot;&gt;Scope
                of variables in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/arithmetic-operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Arithmetic
                operators in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;Java if
                else statement&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&quot; target=&quot;_blank&quot;&gt;Java while
                loop&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/java-do-while-loop.html&quot; target=&quot;_blank&quot;&gt;Do while
                loop in Java&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/4649373657770502963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-break-statement.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4649373657770502963'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4649373657770502963'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-break-statement.html' title='Java break statement'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGMPIPGSAp50uq-M5ArOvogQkQIVV7OKsi1Tb3B_ixDHor30huCtaQRw81wllOvjiT9tX8zdIbtpZ7qPppMAb-bdkwRmfoGWMrHsPmc2DNM0ldUXluOkuMDoIgMo-pjD_ixw4gv0HaqHA/s72-c/java-break-statement.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-2198367904612482294</id><published>2018-11-03T14:14:00.000+05:30</published><updated>2018-11-03T17:04:49.229+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java do while loop</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
    &lt;img alt=&quot;Do while loop in Java&quot; title=&quot;Do while loop in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4xmKuwfXztvugTkmwk0X4G4kdvFjJW7nxfYNmsXkzwALORTw2Z3Ztf-Iha9gHxd20LSBzFXUt1Uc_8d2_DwYAUBCxKqGrxJCcl1BbAJ8DTP4l9IFb5Ip9Gh0PTq8ogEvySb0A_DIn6Xw/s1600/do-while-loop-in-java.jpg&quot;/&gt;
    &lt;p&gt;As you saw in &lt;a href=&quot;http://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&quot; target=&quot;_blank&quot;&gt;Java
        while loop&lt;/a&gt;, if the condition expression in &lt;a
            href=&quot;http://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&quot; target=&quot;_blank&quot;&gt;while loop&lt;/a&gt; is
        &lt;code&gt;false&lt;/code&gt; initially, then the body of the loop will not be executed at all. However, sometimes it is
        desirable to execute the body of a loop at least once, even if the conditional expression is &lt;code&gt;false&lt;/code&gt;
        to begin with.&lt;/p&gt;
    &lt;p&gt;The Java do while loop is a control flow statement that executes the program at least once and the
        further execution depends on given boolean condition.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;do {
// code to be executed
} while (condition);
&lt;/pre&gt;
    &lt;p&gt;Each iteration of the &lt;b&gt;do-while&lt;/b&gt; loop first executes the body of the loop and then evaluates
        the conditional expression. If this expression is &lt;code&gt;true&lt;/code&gt;, the loop will repeat. Otherwise, the
        loop terminates. As with all of Java’s loops, &lt;i&gt;condition&lt;/i&gt; must be a Boolean expression.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Flowchart:&lt;/b&gt;&lt;/p&gt;
    &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxs4HUT6e2RG6CmNB8v625Ew23VahrxOmdfLuqoMsses1McUzIu4SZ6TwTqPFfB1DyEx12JgP1T9lHDPvOCF_NeJBaB3bD860WV0RPuGll1tt0T8Q-M0j5QOsp-pp1qAItWToiEsXChz4/s1600/do-while-loop-flowchart.jpg&quot;
       imageanchor=&quot;1&quot;&gt;&lt;img alt=&quot;Do while loop flowchart&quot; title=&quot;Do while loop flowchart&quot; border=&quot;0&quot;
                            src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxs4HUT6e2RG6CmNB8v625Ew23VahrxOmdfLuqoMsses1McUzIu4SZ6TwTqPFfB1DyEx12JgP1T9lHDPvOCF_NeJBaB3bD860WV0RPuGll1tt0T8Q-M0j5QOsp-pp1qAItWToiEsXChz4/s1600/do-while-loop-flowchart.jpg&quot;
                            data-original-width=&quot;700&quot; data-original-height=&quot;300&quot;/&gt;&lt;/a&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;  title:&#39;DoWhileLoopDemo.java&#39;&quot;&gt;// demonstrate the do-while loop
public class DoWhileLoopDemo {

  public static void main(String[] args) {
    int i = 10;

    do {
      System.out.println(&amp;quot;i= &amp;quot; + i);
      i--; // decrease value of i by 1
    } while (i &amp;gt;= 0);
  }
}&lt;/pre&gt;
    &lt;p&gt;You can write above program more efficiently as below:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;  title:&#39;DoWhileLoopDemo.java&#39;&quot;&gt;// demonstrate the do-while loop
public class DoWhileLoopDemo {

  public static void main(String[] args) {
    int i = 10;

    do {
      System.out.println(&amp;quot;i= &amp;quot; + i);
    } while (--i &amp;gt;= 0);
  }
}&lt;/pre&gt;
    &lt;p&gt;In this example, the expression &lt;b&gt;(--i &gt; 0)&lt;/b&gt; combines the decrement of &lt;b&gt;i&lt;/b&gt; and the test for zero
        into one expression. Here is how it works. First, the &lt;i&gt;--i&lt;/i&gt; statement executes, decrementing
        &lt;b&gt;i&lt;/b&gt; and returning the new value of &lt;b&gt;i&lt;/b&gt;. This value is then compared with zero. If it is greater
        than or equal to zero, the loop continues; otherwise, it terminates.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;i= 10
i= 9
i= 8
i= 7
i= 6
i= 5
i= 4
i= 3
i= 2
i= 1
i= 0&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java infinite do-while loop&lt;/h2&gt;
    &lt;p&gt;If you pass &lt;code&gt;true&lt;/code&gt; or boolean expression that will be always &lt;code&gt;true&lt;/code&gt; after evaluation in the
        do-while loop, it will be infinite loop.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;do {
// code to be executed
} while (true);&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;InfiniteDoWhileLoop.java&#39;&quot;&gt;// demonstrate the infinite do-while loop
public class InfiniteDoWhileLoop {
  public static void main(String[] args) {

    int i = 0, j = 0;
    do {
      System.out.println(&amp;quot;Infinite while loop..&amp;quot;);

      // post increment operator
      i++;
      j++;
    } while (i == j); // value of i and j will be always same
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Infinite while loop..
Infinite while loop..
Infinite while loop..
Infinite while loop..
Infinite while loop..
ctrl+c&lt;/pre&gt;
    &lt;p&gt;Now, you need to press &lt;code&gt;ctrl+c&lt;/code&gt; to exit from the program.&lt;/p&gt;
    &lt;p&gt;The &lt;b&gt;do-while&lt;/b&gt; loop is especially useful when you process a menu selection, because you will
        usually want the body of a menu loop to execute at least once.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;Menu.java&#39;&quot;&gt;// Using a do-while to process a menu selection
class Menu {

  public static void main(String args[]) throws java.io.IOException {
    char choice;

    do {
      System.out.println(&amp;quot;Enter either 1, 2, 3, 4 or 5 for help:&amp;quot;);
      System.out.println(&amp;quot; 1. if&amp;quot;);
      System.out.println(&amp;quot; 2. switch&amp;quot;);
      System.out.println(&amp;quot; 3. while&amp;quot;);
      System.out.println(&amp;quot; 4. do-while&amp;quot;);
      System.out.println(&amp;quot; 5. for\n&amp;quot;);
      System.out.println(&amp;quot;Choose one:&amp;quot;);
      choice = (char) System.in.read();
    } while (choice &amp;lt; &#39;1&#39; || choice &amp;gt; &#39;5&#39;);

    System.out.println(&amp;quot;\n&amp;quot;);
    switch (choice) {
      case &#39;1&#39;:
        System.out.println(&amp;quot;The if:\n&amp;quot;);
        System.out.println(&amp;quot;if(condition) statement;&amp;quot;);
        System.out.println(&amp;quot;else statement;&amp;quot;);
        break;
      case &#39;2&#39;:
        System.out.println(&amp;quot;The switch:\n&amp;quot;);
        System.out.println(&amp;quot;switch(expression) {&amp;quot;);
        System.out.println(&amp;quot; case constant:&amp;quot;);
        System.out.println(&amp;quot; statement sequence&amp;quot;);
        System.out.println(&amp;quot; break;&amp;quot;);
        System.out.println(&amp;quot; //...&amp;quot;);
        System.out.println(&amp;quot;}&amp;quot;);
        break;
      case &#39;3&#39;:
        System.out.println(&amp;quot;The while:\n&amp;quot;);
        System.out.println(&amp;quot;while(condition) statement;&amp;quot;);
        break;
      case &#39;4&#39;:
        System.out.println(&amp;quot;The do-while:\n&amp;quot;);
        System.out.println(&amp;quot;do {&amp;quot;);
        System.out.println(&amp;quot; statement;&amp;quot;);
        System.out.println(&amp;quot;} while (condition);&amp;quot;);
        break;
      case &#39;5&#39;:
        System.out.println(&amp;quot;The for:\n&amp;quot;);
        System.out.print(&amp;quot;for(init; condition; iteration)&amp;quot;);
        System.out.println(&amp;quot; statement;&amp;quot;);
        break;
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Enter either 1, 2, 3, 4 or 5 for help:
 1. if
 2. switch
 3. while
 4. do-while
 5. for

Choose one:
2


The switch:

switch(expression) {
 case constant:
 statement sequence
 break;
 //...
}
&lt;/pre&gt;
    &lt;p&gt;In the above program, &lt;b&gt;System.in.read( )&lt;/b&gt; is used to read character from keyboard. &lt;b&gt;System.in.read( )&lt;/b&gt;
        is used here to obtain the user’s choice. It reads characters from standard input (returned as integers,
        which is why the return value was cast to &lt;b&gt;char&lt;/b&gt;). By default, standard input is line buffered, so
        you must press enter before any characters that you type will be sent to your program.&lt;/p&gt;
    &lt;p&gt;One other point to consider: Because &lt;b&gt;System.in.read( )&lt;/b&gt; is being used, the program must specify the
        throws &lt;b&gt;java.io.IOException&lt;/b&gt; clause.&lt;/p&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/what-are-different-ways-to-load-class.html&quot;
                   target=&quot;_blank&quot;&gt;How to load class in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/method-hiding.html&quot; target=&quot;_blank&quot;&gt;Method hiding in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/factorial-of-large-number.html&quot; target=&quot;_blank&quot;&gt;Factorial
                of arbitrary large number&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-stringtokenizer.html&quot; target=&quot;_blank&quot;&gt;Java
                String Tokenizer example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-hashtable.html&quot; target=&quot;_blank&quot;&gt;Hash table in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-enummap.html&quot; target=&quot;_blank&quot;&gt;Enum Map in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-identityhashmap.html&quot; target=&quot;_blank&quot;&gt;Identity
                Hash Map in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-hashmap-example.html&quot; target=&quot;_blank&quot;&gt;Hash Map
                in Java example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-enumset.html&quot; target=&quot;_blank&quot;&gt;Enum Set in
                Java&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/2198367904612482294/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-do-while-loop.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2198367904612482294'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/2198367904612482294'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-do-while-loop.html' title='Java do while loop'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4xmKuwfXztvugTkmwk0X4G4kdvFjJW7nxfYNmsXkzwALORTw2Z3Ztf-Iha9gHxd20LSBzFXUt1Uc_8d2_DwYAUBCxKqGrxJCcl1BbAJ8DTP4l9IFb5Ip9Gh0PTq8ogEvySb0A_DIn6Xw/s72-c/do-while-loop-in-java.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-4530315724385474971</id><published>2018-11-03T12:52:00.000+05:30</published><updated>2018-11-03T17:04:14.664+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>While loop in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
    &lt;img alt=&quot;While loop in Java&quot; title=&quot;While loop in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOmvku1_R4jvuKDaZVaOuNWyJ_5gCyrGmm_tRRtM6OMJIn_wkQVCDZVVDHmH4-W4PWW6VBX0Zk_tSz4w7NhnuiHBcNNrUanfpcRp2vUhmx6xHHstme4NbJHDgyUy1pz09j2w_4AaXeOxA/s1600/while-loop-in-java.jpg&quot;/&gt;
    &lt;p&gt;The Java while loop is a control flow statement that controls the execution of the programs repeatedly
        on the basis of given boolean condition.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;while (condition) {
// code to be executed
}&lt;/pre&gt;
    &lt;p&gt;The &lt;i&gt;condition&lt;/i&gt; can be any boolean expression. The body of the loop will be executed as long
        as the conditional expression is
        &lt;code&gt;true&lt;/code&gt;
        . When &lt;i&gt;condition&lt;/i&gt; becomes &lt;code&gt;false&lt;/code&gt;, control passes to the
        next line of code immediately following the loop. The curly braces are unnecessary if only a
        single statement is being repeated.
    &lt;/p&gt;
&lt;p&gt;&lt;b&gt;Flowchart:&lt;/b&gt;&lt;/p&gt;
    &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxJvQb2Z_OguYFna14VJiXLRkvFf9S4VG_X7kLY2T1SjDOkeXN-T5LD9w7bfohD7Wnb7vG6SL0ssKaRi8dbGy3QKDnr57jS3dXtGKEnxAExRA8yVFcmDENmmW40pJ5yHnNkqDIpZWF_UQ/s1600/while-loop.jpg&quot;
       imageanchor=&quot;1&quot;&gt;&lt;img  alt=&quot;Flowchart diagram of while loop in Java&quot; title=&quot;Flowchart diagram of while loop in Java&quot; border=&quot;0&quot;
                            src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxJvQb2Z_OguYFna14VJiXLRkvFf9S4VG_X7kLY2T1SjDOkeXN-T5LD9w7bfohD7Wnb7vG6SL0ssKaRi8dbGy3QKDnr57jS3dXtGKEnxAExRA8yVFcmDENmmW40pJ5yHnNkqDIpZWF_UQ/s1600/while-loop.jpg&quot;
                            data-original-width=&quot;720&quot; data-original-height=&quot;540&quot;/&gt;&lt;/a&gt;
    &lt;!--image of while loop--&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;  title:&#39;WhileLoopDemo.java&#39;&quot;&gt;// demonstrate the while loop
public class WhileLoopDemo {

  public static void main(String[] args) {
    int i = 10;

    while (i &amp;gt;= 0) {
      System.out.println(&amp;quot;i= &amp;quot; + i);
      i--; // decrease value of i by 1
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;i= 10
i= 9
i= 8
i= 7
i= 6
i= 5
i= 4
i= 3
i= 2
i= 1
i= 0&lt;/pre&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Java infinite while loop&lt;/h2&gt;
    &lt;p&gt;If you pass &lt;code&gt;true&lt;/code&gt; or boolean expression that will be always &lt;code&gt;true&lt;/code&gt; after evaluation in the
        while loop,
        it will be infinite loop.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;while (true) {
// code to be executed
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;InfiniteWhileLoop.java&#39;&quot;&gt;// demonstrate the infinite while loop
public class InfiniteWhileLoop {
  public static void main(String[] args) {

    int i = 0, j = 0;
    while (i == j) { // value of i and j will be always same
      System.out.println(&amp;quot;Infinite while loop..&amp;quot;);

      // post increment operator
      i++;
      j++;
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Infinite while loop..
Infinite while loop..
Infinite while loop..
Infinite while loop..
Infinite while loop..
ctrl+c&lt;/pre&gt;
    &lt;p&gt;Now, you need to press &lt;code&gt;ctrl+c&lt;/code&gt; to exit from the program.&lt;/p&gt;
    &lt;h2 style=&quot;color: crimson&quot;&gt;Empty body of while loop&lt;/h2&gt;
    &lt;p&gt;The body of the &lt;b&gt;while&lt;/b&gt; (or any other of Java’s loops) can be empty. This is because a &lt;i&gt;null
        statement&lt;/i&gt; (one that consists only of a semicolon) is syntactically valid in Java. For example,
        consider the following program:&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;NoBody.java&#39;&quot;&gt;/*Loop can have empty body.
 * Demonstrate the while loop with empty body*/
class NoBody {
  public static void main(String args[]) {
    int i, j;
    i = 100;
    j = 200;

    // find midpoint between i and j

    while (++i &amp;lt; --j) ; // no body in this loop
    System.out.println(&amp;quot;Midpoint is &amp;quot; + i);
  }
}&lt;/pre&gt;
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Midpoint is 150&lt;/pre&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/what-are-different-ways-to-load-class.html&quot;
                   target=&quot;_blank&quot;&gt;How to load class in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/method-hiding.html&quot; target=&quot;_blank&quot;&gt;Method hiding in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/factorial-of-large-number.html&quot; target=&quot;_blank&quot;&gt;Factorial
                of arbitrary large number&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-stringtokenizer.html&quot; target=&quot;_blank&quot;&gt;Java
                String Tokenizer example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-hashtable.html&quot; target=&quot;_blank&quot;&gt;Hash table in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-enummap.html&quot; target=&quot;_blank&quot;&gt;Enum Map in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-identityhashmap.html&quot; target=&quot;_blank&quot;&gt;Identity
                Hash Map in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-hashmap-example.html&quot; target=&quot;_blank&quot;&gt;Hash Map
                in Java example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-enumset.html&quot; target=&quot;_blank&quot;&gt;Enum Set in
                Java&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/4530315724385474971/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4530315724385474971'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/4530315724385474971'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html' title='While loop in Java'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOmvku1_R4jvuKDaZVaOuNWyJ_5gCyrGmm_tRRtM6OMJIn_wkQVCDZVVDHmH4-W4PWW6VBX0Zk_tSz4w7NhnuiHBcNNrUanfpcRp2vUhmx6xHHstme4NbJHDgyUy1pz09j2w_4AaXeOxA/s72-c/while-loop-in-java.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-1859673120585507722</id><published>2018-11-01T20:55:00.000+05:30</published><updated>2018-11-01T20:55:05.305+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Switch statement in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
    &lt;img alt=&quot;Switch statement in Java&quot; title=&quot;Switch statement in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3DN41GRAwO-8auz4OUxGzLsSWyHNF60GtG98yG8Vd12Pafan-mOpgJAxrtKP9efeuprGBl56iqrP9ejAdKiltr2RXFg9XqS9AY04XbReMFkjXJcHtkRO9nHwTvRyLj6OBA_SdsJXHnuc/s1600/java-switch-statement.jpg&quot;/&gt;
    &lt;p&gt;The switch statement is Java’s multiway branch statement. It provides an easy way to
        dispatch execution to different parts of your code based on the value of an expression. Here is general form of
        switch statement:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;switch (expression) {
case value1:
// statement sequence
break; // optional
case value2:
// statement sequence
break; // optional
......
......
......
case valueN :
// statement sequence
break; // optional
default:
// default statement sequence
}&lt;/pre&gt;
    &lt;p&gt;Switch provides better alternative than a large series of &lt;b class=&quot;java&quot;&gt;&lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/10/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;if-else-if&lt;/a&gt;&lt;/b&gt;
        statements. Prior to JDK
        7, &lt;i&gt;expression&lt;/i&gt; must be of type &lt;b class=&quot;java&quot;&gt;byte&lt;/b&gt;, &lt;b class=&quot;java&quot;&gt;short&lt;/b&gt;, &lt;b
                class=&quot;java&quot;&gt;int&lt;/b&gt;, &lt;b class=&quot;java&quot;&gt;char&lt;/b&gt;, &lt;b class=&quot;java&quot;&gt;Byte&lt;/b&gt;, &lt;b class=&quot;java&quot;&gt;Short&lt;/b&gt;, &lt;b
                class=&quot;java&quot;&gt;Integer&lt;/b&gt;, &lt;b class=&quot;java&quot;&gt;Character&lt;/b&gt; or &lt;b class=&quot;java&quot;&gt;&lt;a
                href=&quot;http://walkingtechie.blogspot.com/2017/01/java-enum.html&quot; target=&quot;_blank&quot;&gt;enum&lt;/a&gt;&lt;/b&gt;. Beginning
        with JDK
        7, &lt;i&gt;expression&lt;/i&gt;
        can also be of type &lt;b class=&quot;java&quot;&gt;String&lt;/b&gt;.&lt;/p&gt;
    &lt;p&gt;Important point to be noted:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;There can be one or N number of case values for a switch expression.&lt;/li&gt;
        &lt;li&gt;Each value specified in case statement must be unique. In case of duplicate value, it renders compile-time
            error.
        &lt;/li&gt;
        &lt;li&gt;Type of each value must be compatible with the type of &lt;i&gt;expression&lt;/i&gt;. The value of case must be unique
            constant expression or literal.
        &lt;/li&gt;
        &lt;li&gt;The Java switch expression must be of byte, short, int, char (with its Wrapper type), &lt;a
                href=&quot;http://walkingtechie.blogspot.com/2017/01/java-enum.html&quot; target=&quot;_blank&quot;&gt;enums&lt;/a&gt; and string.
        &lt;/li&gt;
        &lt;li&gt;Each case statement can have a break statement which is optional. When control reaches to the break
            statement, it jumps the control after the switch expression. If a break statement is not found, it executes
            all the subsequent cases.
        &lt;/li&gt;
        &lt;li&gt;If none of the constants matches the value of the expression, then the default statement is executed.&lt;/li&gt;
        &lt;li&gt;Switch can have optional default statement.&lt;/li&gt;
        &lt;li&gt;If no case matches and no default is present, then no further action is taken.
        &lt;/li&gt;
        &lt;li&gt;The &lt;b class=&quot;java&quot;&gt;break&lt;/b&gt; statement is used inside the switch to terminate a statement sequence.&lt;/li&gt;
    &lt;/ul&gt;
    &lt;pre class=&quot;brush:java; title:&#39;SwitchDemo.java&#39;&quot;&gt;// demonstration of Switch
public class SwitchDemo {
  public static void main(String[] args) {
    // variable for switch expression
    int exp = 10;

    switch (exp) {
      case 5:
        System.out.println(&amp;quot;5&amp;quot;);
        break;
      case 10:
        System.out.println(&amp;quot;10&amp;quot;);
        break;
      case 15:
        System.out.println(&amp;quot;15&amp;quot;);
        break;
      default: // default case statement
        System.out.println(&amp;quot;Not in 5, 10 or 15&amp;quot;);
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre&gt;10&lt;/pre&gt;
    &lt;p&gt;Lets see another example of switch statement inside for loop.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;SampleSwitch.java&#39;&quot;&gt;// A simple example of the switch.
class SampleSwitch {
  public static void main(String args[]) {

    for (int i = 0; i &amp;lt; 6; i++)
      switch (i) {
        case 0:
          System.out.println(&amp;quot;i is zero.&amp;quot;);
          break;
        case 1:
          System.out.println(&amp;quot;i is one.&amp;quot;);
          break;
        case 2:
          System.out.println(&amp;quot;i is two.&amp;quot;);
          break;
        case 3:
          System.out.println(&amp;quot;i is three.&amp;quot;);
          break;
        default:
          System.out.println(&amp;quot;i is greater than 3.&amp;quot;);
      }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.&lt;/pre&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;Java switch statement is fall-through&lt;/h2&gt;
    &lt;p&gt;The &lt;b class=&quot;java&quot;&gt;break&lt;/b&gt; statement is optional. If you omit the break, execution will continue on into the
        next case. It is sometimes desirable to have multiple cases without break statements between them.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;MissingBreak.java&#39;&quot;&gt;// In a switch, break statements are optional.
class MissingBreak {
  public static void main(String args[]) {
    for (int i = 0; i &amp;lt; 12; i++)
      switch (i) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
          System.out.println(&amp;quot;i is less than 5&amp;quot;);
          break;
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
          System.out.println(&amp;quot;i is less than 10&amp;quot;);
          break;
        default:
          System.out.println(&amp;quot;i is 10 or more&amp;quot;);
      }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more&lt;/pre&gt;
    &lt;p&gt;Lets see more practical example&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;SeasonSwitch.java&#39;&quot;&gt;public class SeasonSwitch {
  public static void main(String args[]) {
    int month = 4;

    String season;

    switch (month) {
      case 12:
      case 1:
      case 2:
        season = &amp;quot;Winter&amp;quot;;
        break;
      case 3:
      case 4:
      case 5:
        season = &amp;quot;Spring&amp;quot;;
        break;
      case 6:
      case 7:
      case 8:
        season = &amp;quot;Summer&amp;quot;;
        break;
      case 9:
      case 10:
      case 11:
        season = &amp;quot;Autumn&amp;quot;;
        break;
      default:
        season = &amp;quot;Bogus Month&amp;quot;;
    }
    System.out.println(&amp;quot;April is in the &amp;quot; + season + &amp;quot;.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;April is in the Spring.&lt;/pre&gt;
    &lt;pre class=&quot;brush:java; title:&#39;SwitchFallThrough.java&#39;&quot;&gt;public class SwitchFallThrough {
  public static void main(String[] args) {
    int exp = 10;

    switch (exp) {
      case 5:
        System.out.println(&amp;quot;5&amp;quot;);
      case 10:
        System.out.println(&amp;quot;10&amp;quot;);
      case 15:
        System.out.println(&amp;quot;15&amp;quot;);
      case 20:
        System.out.println(&amp;quot;20&amp;quot;);
      case 25:
        System.out.println(&amp;quot;25&amp;quot;);
      default: // default case statement
        System.out.println(&amp;quot;Default statement.&amp;quot;);
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;10
15
20
25
Default statement.&lt;/pre&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;Java switch statement with String&lt;/h2&gt;
    &lt;p&gt;Beginning with JDK 7, you can use &lt;b class=&quot;java&quot;&gt;String&lt;/b&gt; to control a switch statement.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;StringSwitch.java&#39;&quot;&gt;class StringSwitch {
  public static void main(String args[]) {
    String str = &amp;quot;two&amp;quot;;
    switch (str) {
      case &amp;quot;one&amp;quot;:
        System.out.println(&amp;quot;one&amp;quot;);
        break;
      case &amp;quot;two&amp;quot;:
        System.out.println(&amp;quot;two&amp;quot;);
        break;
      case &amp;quot;three&amp;quot;:
        System.out.println(&amp;quot;three&amp;quot;);
        break;
      default:
        System.out.println(&amp;quot;no match&amp;quot;);
        break;
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;two&lt;/pre&gt;
    &lt;p&gt;Switching on strings can be more expensive than switching on integers.
        Therefore, it is best to switch on strings only in cases in which the controlling data is already
        in string form. In other words, don’t use strings in a switch unnecessarily.&lt;/p&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;Java Nested Switch Statement&lt;/h2&gt;
    &lt;p&gt;You can use switch statement inside other switch statement in Java. It is known as nested switch statement. Since
        a switch statement defines its own block, no conflicts arise between the
        case constants in the inner switch and those in the outer switch.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;NestedSwitch.java&#39;&quot;&gt;public class NestedSwitch {
  public static void main(String[] args) {
    int exp = 1, target = 0;
    switch (exp) {
      case 1:
        switch (target) { // nested switch
          case 0:
            System.out.println(&amp;quot;Inner switch exp: 1&amp;quot;);
            System.out.println(&amp;quot;target is zero&amp;quot;);
            break;
          case 1: // no conflicts with outer switch
            System.out.println(&amp;quot;target is one&amp;quot;);
            break;
        }
        System.out.println(&amp;quot;Outer switch exp: 1&amp;quot;);
        break;
      case 2:
        target = 3;
        System.out.println(&amp;quot;Outer switch: 2&amp;quot;);
        break;
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;Inner switch exp: 1
target is zero
Outer switch exp: 1&lt;/pre&gt;
    &lt;p&gt;In summary, there are three important features of the switch statement to note:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;The switch differs from the &lt;b&gt;&lt;a
                href=&quot;https://walkingtechie.blogspot.com/2018/10/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;if&lt;/a&gt;&lt;/b&gt;
            in that switch can only test for equality, whereas &lt;b&gt;&lt;a
                    href=&quot;https://walkingtechie.blogspot.com/2018/10/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;if&lt;/a&gt;&lt;/b&gt;
            can
            evaluate any type of Boolean expression. That is, the switch looks only for a match
            between the value of the expression and one of its &lt;b&gt;case&lt;/b&gt; constants.
        &lt;/li&gt;
        &lt;li&gt;No two case constants in the same switch can have identical values. Of course, a
            switch statement and an enclosing outer switch can have case constants in common.
        &lt;/li&gt;
        &lt;li&gt;A switch statement is usually more efficient than a set of nested &lt;b&gt;&lt;a
                href=&quot;https://walkingtechie.blogspot.com/2018/10/java-if-else-statement.html&quot; target=&quot;_blank&quot;&gt;if&lt;/a&gt;&lt;/b&gt;s.
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;Java using enum with switch statement&lt;/h2&gt;
    &lt;p&gt;Java allow to use &lt;a
            href=&quot;http://walkingtechie.blogspot.com/2017/01/java-enum.html&quot; target=&quot;_blank&quot;&gt;enum&lt;/a&gt; with switch
        statement. Lets see an example:&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;EnumSwitchDemo.java&#39;&quot;&gt;public class EnumSwitchDemo {
  public enum Days {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
  }

  public static void main(String[] args) {
    Days[] days = Days.values();
    for (Days day : days) {
      switch (day) {
        case MONDAY:
          System.out.println(&amp;quot;Monday&amp;quot;);
          break;
        case TUESDAY:
          System.out.println(&amp;quot;Tuesday&amp;quot;);
          break;
        case WEDNESDAY:
          System.out.println(&amp;quot;Wednesday&amp;quot;);
          break;
        case THURSDAY:
          System.out.println(&amp;quot;Thursday&amp;quot;);
          break;
        case FRIDAY:
          System.out.println(&amp;quot;friday&amp;quot;);
          break;
        case SATURDAY:
          System.out.println(&amp;quot;Saturday&amp;quot;);
          break;
        case SUNDAY:
          System.out.println(&amp;quot;Sunday&amp;quot;);
          break;
      }
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;Sunday
Monday
Tuesday
Wednesday
Thursday
friday
Saturday&lt;/pre&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;Java using wrapper with switch statement&lt;/h2&gt;
    &lt;p&gt;Java allows to use four wrapper classes with swith statement. Wrapper classes are &lt;b class=&quot;java&quot;&gt;Character&lt;/b&gt;,
        &lt;b
                class=&quot;java&quot;&gt;Byte&lt;/b&gt;, &lt;b class=&quot;java&quot;&gt;Short&lt;/b&gt;, and &lt;b class=&quot;java&quot;&gt;Integer&lt;/b&gt;.&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;WrapperSwitchDemo.java&#39;&quot;&gt;public class WrapperSwitchDemo {
  public static void main(String[] args) {
    // example of Character wrapper class
    Character exp = new Character(&#39;a&#39;);
    switch (exp) {
      case &#39;a&#39;:
        System.out.println(&amp;quot;a&amp;quot;);
        break;
      case &#39;b&#39;:
        System.out.println(&amp;quot;b&amp;quot;);
        break;
      default:
        System.out.println(&amp;quot;Other character.&amp;quot;);
    }

    // example of Byte wrapper class with switch statement
    Byte age = new Byte((byte) 18);
    switch (age) {
      case 16:
        System.out.println(&amp;quot;You are under 18.&amp;quot;);
        break;
      case 18:
        System.out.println(&amp;quot;You are eligible for vote.&amp;quot;);
        break;
      case 65:
        System.out.println(&amp;quot;You are senior citizen.&amp;quot;);
        break;
      default:
        System.out.println(&amp;quot;Please give the valid age.&amp;quot;);
        break;
    }

    // example of Short wrapper class with switch statement
    Short age1 = new Short((short) 65);
    switch (age1) {
      case 16:
        System.out.println(&amp;quot;You are under 18.&amp;quot;);
        break;
      case 18:
        System.out.println(&amp;quot;You are eligible for vote.&amp;quot;);
        break;
      case 65:
        System.out.println(&amp;quot;You are senior citizen.&amp;quot;);
        break;
      default:
        System.out.println(&amp;quot;Please give the valid age.&amp;quot;);
        break;
    }
    // example of Integer wrapper class with switch statement
    Integer age2 = new Integer(16);
    switch (age2) {
      case 16:
        System.out.println(&amp;quot;You are under 18.&amp;quot;);
        break;
      case 18:
        System.out.println(&amp;quot;You are eligible for vote.&amp;quot;);
        break;
      case 65:
        System.out.println(&amp;quot;You are senior citizen.&amp;quot;);
        break;
      default:
        System.out.println(&amp;quot;Please give the valid age.&amp;quot;);
        break;
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;a
You are eligible for vote.
You are senior citizen.
You are under 18.&lt;/pre&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;How Java compiler works with switch statement&lt;/h2&gt;
    &lt;p&gt;When it compiles a switch statement, the Java compiler will inspect each of the case
        constants and create a &quot;jump table&quot; that it will use for selecting the path of execution
        depending on the value of the expression. Therefore, if you need to select among a large group of values, a
        switch statement will run much faster than the equivalent logic coded using
        a sequence of if-elses. The compiler can do this because it knows that the case constants are
        all the same type and simply must be compared for equality with the switch expression. The
        compiler has no such knowledge of a long list of if expressions.&lt;/p&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/what-are-different-ways-to-load-class.html&quot;
                   target=&quot;_blank&quot;&gt;How to load class in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/factorial-of-large-number.html&quot; target=&quot;_blank&quot;&gt;Factorial
                of arbitrary large number&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-comments.html&quot; target=&quot;_blank&quot;&gt;Comments in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-stringtokenizer.html&quot; target=&quot;_blank&quot;&gt;Java
                String Tokenizer example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-hashtable.html&quot; target=&quot;_blank&quot;&gt;Hash table in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-enummap.html&quot; target=&quot;_blank&quot;&gt;Enum Map in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/design-patterns-set-1-introduction.html&quot; target=&quot;_blank&quot;&gt;Design patterns in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-hashmap-example.html&quot; target=&quot;_blank&quot;&gt;Hash Map
                in Java example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/08/linear-search-or-sequential-search.html&quot; target=&quot;_blank&quot;&gt;Linear search or sequential search&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/1859673120585507722/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/switch-statement-in-java.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/1859673120585507722'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/1859673120585507722'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/switch-statement-in-java.html' title='Switch statement in Java'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3DN41GRAwO-8auz4OUxGzLsSWyHNF60GtG98yG8Vd12Pafan-mOpgJAxrtKP9efeuprGBl56iqrP9ejAdKiltr2RXFg9XqS9AY04XbReMFkjXJcHtkRO9nHwTvRyLj6OBA_SdsJXHnuc/s72-c/java-switch-statement.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-8968286161931062122</id><published>2018-11-01T20:53:00.000+05:30</published><updated>2018-11-01T20:53:52.350+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java if-else statement</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
    &lt;img alt=&quot;if else statement in Java&quot; title=&quot;if else statement in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixSofeZD4qHieSDQoEupeESYzvcsW2DZhRRMLnZ9QX3nN2ry8kJUYuVEgZszrOYjQV_8qGyJ0FS-aoEsYJGpbHfuppfkTyj6mDSjNPXcnV9J63DwuZ2YY6uZQaMNkr2wuKofZRY6fsJdQ/s1600/if-else-statement.jpg&quot;/&gt;
    &lt;p&gt;Java &lt;b class=&quot;java&quot;&gt;if&lt;/b&gt; statement is used to control the flow of program&#39;s execution based upon conditions.
        Condition evaluated to either &lt;b class=&quot;java&quot;&gt;true&lt;/b&gt; or &lt;b class=&quot;java&quot;&gt;false&lt;/b&gt;. There are various type of
        if statements in Java:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;if statement&lt;/li&gt;
        &lt;li&gt;if-else statement&lt;/li&gt;
        &lt;li&gt;if-else-if ladder&lt;/li&gt;
        &lt;li&gt;nested if statement&lt;/li&gt;
    &lt;/ul&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;Java if statement&lt;/h2&gt;
    &lt;p&gt;The if statement is Java’s conditional branch statement. It can be used to route program execution through
        two different paths.&lt;/p&gt;
    &lt;p&gt;Java if statement test the condition. It execute the &lt;i&gt;if statement&lt;/i&gt; or &lt;i&gt;if block&lt;/i&gt; only if
        &lt;i&gt;condition&lt;/i&gt; is true. If &lt;i&gt;condition&lt;/i&gt; is false then statement or block of statements is bypassed. Here
        is the general form of the if statement:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;if (condition) statement;&lt;/pre&gt;
    OR
    &lt;pre class=&quot;brush:java;&quot;&gt;if (condition) {
//.....
//code to be executed
//.....
}&lt;/pre&gt;
    &lt;p&gt;You can see an example of &lt;a href=&quot;http://walkingtechie.blogspot.com/2018/10/java-if-statement.html&quot;
                                    target=&quot;_blank&quot;&gt;if(condition) statement.&lt;/a&gt;&lt;/p&gt;
    &lt;!--Add image of if flow --&gt;
    &lt;pre class=&quot;brush:java; title:&#39;IfDemo.java&#39;&quot;&gt;// demonstrate If statement
public class IfDemo {
  public static void main(String[] args) {
    // declare a num variable and initialise 20 to it
    int num = 20;

    // relation expression
    // evaluate to true or false
    if (num &amp;gt; 10) {
      System.out.println(&amp;quot;Num is greater than 10.&amp;quot;);
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Num is greater than 10.&lt;/pre&gt;
    p&gt;We have seen the expression used to control if statement is relational operators. It can control using the
    boolean variable. Example:&lt;/p&gt;
    &lt;pre class=&quot;brush:java; title:&#39;IfBooleanDemo.java&#39;&quot;&gt;// Demonstrate if statement using boolean variable
public class IfBooleanDemo {
  public static void main(String[] args) {
    boolean flag = true;
    int a = 5;

    if (flag) System.out.println(&amp;quot;This is an example of single statement.&amp;quot;);

    if (flag) {
      a = 10;
      a = a &amp;gt;&amp;gt; 2;
      System.out.println(&amp;quot;\nThis is an example of block of statements.&amp;quot;);
      System.out.println(&amp;quot;Value of a = &amp;quot; + a);
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;This is an example of single statement.

This is an example of block of statements.
Value of a = 2&lt;/pre&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;Java if statement&lt;/h2&gt;
    &lt;p&gt;Java if statement test the condition. It execute the &lt;i&gt;if statement&lt;/i&gt; or &lt;i&gt;if block&lt;/i&gt; only if
        &lt;i&gt;condition&lt;/i&gt; is true. If &lt;i&gt;condition&lt;/i&gt; is false then else statement or block of statements is executed.
        Here is the general form of the if-else statement:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;if (condition) statement1;
else statement2;&lt;/pre&gt;
    OR
    &lt;pre class=&quot;brush:java;&quot;&gt;if (condition) {
//.....
//code to be executed
//.....
}else {
// .......
// else code to be executed
// ......
}&lt;/pre&gt;
    &lt;!--Add image of if-else flow --&gt;
    &lt;pre class=&quot;brush:java; title:&#39;IfElseDemo.java&#39;&quot;&gt;// demonstrate if-else statement
public class IfElseDemo {
  public static void main(String[] args) {
    int num = 12;
    // single if-else statement
    if (num % 2 == 0) System.out.println(&amp;quot;num is even.&amp;quot;);
    else System.out.println(&amp;quot;num is odd.&amp;quot;);

    num = 21;
    // single if and block of else statements
    if (num % 2 == 0) System.out.println(&amp;quot;\nnum is even.&amp;quot;);
    else {
      System.out.println(&amp;quot;\nBlock of statements in else block.&amp;quot;);
      System.out.println(&amp;quot;num is odd.&amp;quot;);
    }

    // best practice to write statement&#39;s in opening curly and curly braces
    if (num &amp;gt;= 21) {
      System.out.println(&amp;quot;\nnum is greater than or equal to 21&amp;quot;);

    } else {
      System.out.println(&amp;quot;\nnum is less than 21&amp;quot;);
    }
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;num is even.

Block of statements in else block.
num is odd.

num is greater than or equal to 21&lt;/pre&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;if-else-if ladder&lt;/h2&gt;
    &lt;p&gt;The if-else-if ladder statement executes one of the condition from the multiple statements.&lt;/p&gt;
    &lt;pre class=&quot;brush:java&quot;&gt;if (condition1) {
//code to be executed if condition1 is true
} else if (condition2) {
//code to be executed if condition2 is true
} else if (condition3) {
//code to be executed if condition3 is true
}
...
else {
//code to be executed if all the conditions are false
}&lt;/pre&gt;
    &lt;p&gt;The if statements are executed from the top down. As soon as one of the if conditions is true then the
        corresponding associated if statements is executed, and rest of the ladder is bypassed. If none of the
        conditions is true, then the final else statement will be
        executed. The else act as default condition; it will be executed if all other conditional tests fail. If there
        is no final else and all other conditions are false, then no action will take place.&lt;/p&gt;
    &lt;!--Add image of if-else-if ladder flow --&gt;
    &lt;pre class=&quot;brush:java; title:&#39;IfElseIfLadder.java&#39;&quot;&gt;// Demonstrate if-else-if statements.
class IfElseIfLadder {
  public static void main(String args[]) {

    int month = 4; // April

    String season;

    if (month == 12 || month == 1 || month == 2) season = &amp;quot;Winter&amp;quot;;
    else if (month == 3 || month == 4 || month == 5) season = &amp;quot;Spring&amp;quot;;
    else if (month == 6 || month == 7 || month == 8) season = &amp;quot;Summer&amp;quot;;
    else if (month == 9 || month == 10 || month == 11) season = &amp;quot;Autumn&amp;quot;;
    else season = &amp;quot;Bogus Month&amp;quot;;

    System.out.println(&amp;quot;April is in the &amp;quot; + season + &amp;quot;.&amp;quot;);
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;April is in the Spring.&lt;/pre&gt;
    &lt;h2 style=&quot;color:crimson&quot;&gt;nested if statement&lt;/h2&gt;
    &lt;p&gt;The nested if statement represents the &lt;i&gt;if block&lt;/i&gt; within another &lt;i&gt;if block&lt;/i&gt; or &lt;i&gt;else block&lt;/i&gt;.&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;if (condition) {
  // code to be executed
  if (condition) {
    // code to be executed
  }
} else {
  if (condition) {
    // code to be executed
  }
}&lt;/pre&gt;
    &lt;!--Add image of nested flow --&gt;
    &lt;pre class=&quot;brush:java; title:&#39;NestedIfDemo.java&#39;&quot;&gt;// Demonstrate Nested if
public class NestedIfDemo {
  public static void main(String[] args) {
    int i = 10, j = 30;
    int a = 0, b = -1;
    int k = 120;
    int c = 12, d = 21;

    if (i == 10) {

      if (j &amp;lt; 20) a = b;

      if (k &amp;gt; 100) c = d; // this if is
      else a = c; // associated with this else

    } else a = d; // this else refers to if(i == 10)

    System.out.println(&amp;quot;Value of i = &amp;quot; + i + &amp;quot; j = &amp;quot; + j);
    System.out.println(&amp;quot;Value of a = &amp;quot; + a + &amp;quot; b= &amp;quot; + b);
    System.out.println(&amp;quot;Value of c = &amp;quot; + c + &amp;quot; d= &amp;quot; + d);
    System.out.println(&amp;quot;Value of k = &amp;quot; + k);
  }
}&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre class=&quot;brush:java;&quot;&gt;Value of i = 10 j = 30
Value of a = 0 b= -1
Value of c = 21 d= 21
Value of k = 120&lt;/pre&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Java
                operators&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/boolean-logical-operators-in-java.html&quot;
                   target=&quot;_blank&quot;&gt;Boolean logical operators&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/11/bitwise-operators-in-java.html&quot; target=&quot;_blank&quot;&gt;Bitwise
                operators&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-stringtokenizer.html&quot; target=&quot;_blank&quot;&gt;Java
                String Tokenizer example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-hashtable.html&quot; target=&quot;_blank&quot;&gt;Hash table in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-enummap.html&quot; target=&quot;_blank&quot;&gt;Enum Map in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-comments.html&quot; target=&quot;_blank&quot;&gt;Type of comments
                in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-hashmap-example.html&quot; target=&quot;_blank&quot;&gt;Hash Map
                in Java example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-enumset.html&quot; target=&quot;_blank&quot;&gt;Enum Set in
                Java&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/8968286161931062122/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-if-else-statement.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/8968286161931062122'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/8968286161931062122'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-if-else-statement.html' title='Java if-else statement'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixSofeZD4qHieSDQoEupeESYzvcsW2DZhRRMLnZ9QX3nN2ry8kJUYuVEgZszrOYjQV_8qGyJ0FS-aoEsYJGpbHfuppfkTyj6mDSjNPXcnV9J63DwuZ2YY6uZQaMNkr2wuKofZRY6fsJdQ/s72-c/if-else-statement.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1082035720476633097.post-7775560567260442033</id><published>2018-11-01T20:51:00.000+05:30</published><updated>2018-11-06T00:57:29.178+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java Control statements</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left; font-size: 17px;&quot; trbidi=&quot;on&quot;&gt;
    &lt;img alt=&quot;Control statements in Java&quot; title=&quot;Control statements in Java&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4mpeeBunjyGWT73xo51aJmL5fmLQzmkqrRHibD6NiWacXXy4LK40JXMOrTvLdf-sd5-KUwYmkE85l9UkgwPQQnbY4CjbyL0rAuOVNKo9UywYa_JH8Xz6YsJq0oc74_KBkLQZhyphenhyphenloo9hI/s1600/control-statements.jpg&quot;/&gt;
    &lt;p&gt;A programming language uses control statement to control the flow of execution of a program. Java&#39;s program
        control statement can be categories in three: selection, iteration and jump.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;&lt;i&gt;Selection&lt;/i&gt;&lt;/b&gt; statement allows the different paths of execution based on outcomes of the expression or
        the state of variable.&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;&lt;i&gt;Iteration&lt;/i&gt;&lt;/b&gt; statement allows execution to repeat the one or set of statements(&lt;a
            href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;block of
        code&lt;/a&gt;).&lt;/p&gt;
    &lt;p&gt;&lt;b&gt;&lt;i&gt;Jump&lt;/i&gt;&lt;/b&gt; statement allows program to run in non-linear fashion.&lt;/p&gt;
    &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiSkl7pnZwfRcnZX21z7ahDjcDqqGTjzKGuhMyDi_iyRmEdu52S7TyJrJBur9OpfRFFGI9d9BcUBb0Ib66rvI6wmvKMyR8vSPFe0HUY9XeNbmhyphenhyphenD8wYUYGsNyTlxegpgwrMrmb1-qn_vg/s1600/control-statements-java.jpg&quot;
       imageanchor=&quot;1&quot;&gt;&lt;img alt=&quot;Java Control statements&quot; title=&quot;Java Control statements&quot; border=&quot;0&quot;
                            src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiSkl7pnZwfRcnZX21z7ahDjcDqqGTjzKGuhMyDi_iyRmEdu52S7TyJrJBur9OpfRFFGI9d9BcUBb0Ib66rvI6wmvKMyR8vSPFe0HUY9XeNbmhyphenhyphenD8wYUYGsNyTlxegpgwrMrmb1-qn_vg/s1600/control-statements-java.jpg&quot;
                            data-original-width=&quot;800&quot; data-original-height=&quot;600&quot;/&gt;&lt;/a&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Selection statement&lt;/h1&gt;
    &lt;p&gt;Java supports two selection statements: &lt;b&gt;if&lt;/b&gt; and &lt;b&gt;switch&lt;/b&gt;. These statements allow you to control
        the flow of your program’s execution based upon conditions known only during runtime.&lt;/p&gt;
    &lt;ol&gt;
        &lt;li&gt;if statement
            &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/10/java-if-else-statement.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;switch statement
            &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/11/switch-statement-in-java.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;/ol&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Iteration statement&lt;/h1&gt;
    &lt;p&gt;We call iteration statement as loops. Loop repeatedly execute the same set of instructions until termination
        condition is met. Java&#39;s iteration statement are:&lt;/p&gt;
    &lt;ol&gt;
        &lt;li&gt;while loop
            &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/11/while-loop-in-java.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;do-while loop
            &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/11/java-do-while-loop.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;for loop
            &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/10/java-for-loop.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;/ol&gt;
    &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSsSJc2Iwp2DJPgDYxqWfXs8SNe3KxESoO2M0E3xSzVJ_2JP5Y58Q3oAFRtr-T0hfS0t8pzNVM8GW9aPkk6lWvmb2zSvLbDfXx3bL2mbw1PRK7oTwuLUVbNMISgRNt8sZ-7d9mdSOtZ64/s1600/loops-in-java.jpg&quot;
       imageanchor=&quot;1&quot;&gt;&lt;img alt=&quot;for loop in Java&quot; title=&quot;for loop in Java&quot; border=&quot;0&quot;
                            src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSsSJc2Iwp2DJPgDYxqWfXs8SNe3KxESoO2M0E3xSzVJ_2JP5Y58Q3oAFRtr-T0hfS0t8pzNVM8GW9aPkk6lWvmb2zSvLbDfXx3bL2mbw1PRK7oTwuLUVbNMISgRNt8sZ-7d9mdSOtZ64/s1600/loops-in-java.jpg&quot;
                            data-original-width=&quot;800&quot; data-original-height=&quot;350&quot;/&gt;&lt;/a&gt;
    &lt;div style=&quot;overflow-x: auto;&quot;&gt;
        &lt;table&gt;
            &lt;tr&gt;
                &lt;th&gt;Comparison&lt;/th&gt;
                &lt;th&gt;while loop&lt;/th&gt;
                &lt;th&gt;do while loop&lt;/th&gt;
                &lt;th&gt;for loop&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Introduction&lt;/td&gt;
                &lt;td&gt;The Java while loop is control flow statement that control the execution of the programs repeatedly
                    on
                    the basis of given boolean condition.
                &lt;/td&gt;
                &lt;td&gt;The Java do while loop is control flow statement that executes the program at least once and the
                    further
                    execution depends on given boolean condition.
                &lt;/td&gt;
                &lt;td&gt;The Java for loop is control flow statement that iterates the set of instruction multiple times.
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;when to use&lt;/td&gt;
                &lt;td&gt;If the number of iteration is unknown or not fixed, it is recommended to use while loop.&lt;/td&gt;
                &lt;td&gt;If the number of iteration is not fixed and you must have to execute the set of code at least once,
                    it
                    is recommended to use do-while loop.
                &lt;/td&gt;
                &lt;td&gt;If the number of iteration is known or fixed, it is recommended to use for loop.&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Syntax&lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;while (condition) {
// code to be executed
}
            &lt;/pre&gt;
                &lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;do {
// code to be executed
} while (condition);
            &lt;/pre&gt;
                &lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;for(initialization; condition; increment/decrement) {
// code to be executed
}
            &lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Syntax for infinite loop&lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;while (true) {
// code to be executed
}
            &lt;/pre&gt;
                &lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;do {
// code to be executed
} while (true);
            &lt;/pre&gt;
                &lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;for (; ; ) {
// code to be executed
}
            &lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Example&lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;int i = 0;
while (i &lt;= 5) {
  System.out.println(i);
  i++;
}
            &lt;/pre&gt;
                &lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;int i = 0;
do {
  System.out.println(i);
  i++;
} while (i &lt;= 5);
            &lt;/pre&gt;
                &lt;/td&gt;
                &lt;td&gt;&lt;pre class=&quot;brush:java;&quot;&gt;for (int i = 0; i &lt;= 5; i++) {
  System.out.println(i);
}
            &lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/div&gt;
    &lt;p&gt;We will also see the For-Each version of for loop and nested loops.&lt;/p&gt;
    &lt;h1 style=&quot;color: blue&quot;&gt;Jump statement&lt;/h1&gt;
    &lt;p&gt;Jump statement transfer the control statement to another part of program. Java support three jump statements:&lt;/p&gt;
    &lt;ol&gt;
        &lt;li&gt;break statement &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/11/java-break-statement.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;continue statement &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/11/java-continue-statement.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;return statement &lt;span class=&#39;date-header&#39;&gt;
        &lt;a class=&#39;read-more anchor-hover&#39;
           href=&#39;https://walkingtechie.blogspot.com/2018/11/java-return-statement.html&#39;&gt;Read more
            &amp;#187;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;/ol&gt;
    &lt;div&gt;&lt;h2 style=&#39;font-size:20px; color: #838383&#39;&gt;Recommended Posts:&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/06/what-are-different-ways-to-load-class.html&quot;
                   target=&quot;_blank&quot;&gt;How to load class in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2016/07/factorial-of-large-number.html&quot; target=&quot;_blank&quot;&gt;Factorial
                of arbitrary large number&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/block-of-code-in-java.html&quot; target=&quot;_blank&quot;&gt;Blocks
                of code in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2018/10/java-comments.html&quot; target=&quot;_blank&quot;&gt;Comments in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-stringtokenizer.html&quot; target=&quot;_blank&quot;&gt;Java
                String Tokenizer example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-hashtable.html&quot; target=&quot;_blank&quot;&gt;Hash table in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-enummap.html&quot; target=&quot;_blank&quot;&gt;Enum Map in
                Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/03/java-identityhashmap.html&quot; target=&quot;_blank&quot;&gt;Identity
                Hash Map in Java&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-hashmap-example.html&quot; target=&quot;_blank&quot;&gt;Hash Map
                in Java example&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;https://walkingtechie.blogspot.com/2017/02/java-enumset.html&quot; target=&quot;_blank&quot;&gt;Enum Set in
                Java&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://walkingtechie.blogspot.com/feeds/7775560567260442033/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-control-statements.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/7775560567260442033'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1082035720476633097/posts/default/7775560567260442033'/><link rel='alternate' type='text/html' href='https://walkingtechie.blogspot.com/2018/11/java-control-statements.html' title='Java Control statements'/><author><name>Santosh Kumar</name><uri>http://www.blogger.com/profile/12656665308552000036</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4mpeeBunjyGWT73xo51aJmL5fmLQzmkqrRHibD6NiWacXXy4LK40JXMOrTvLdf-sd5-KUwYmkE85l9UkgwPQQnbY4CjbyL0rAuOVNKo9UywYa_JH8Xz6YsJq0oc74_KBkLQZhyphenhyphenloo9hI/s72-c/control-statements.jpg" height="72" width="72"/><thr:total>1</thr:total></entry></feed>